private void ButtonCustomarinfoEditClick(object sender, System.Windows.RoutedEventArgs e)
{
ByteToImage(fileName,bytesOfImage,fileSize);
}
private ImageSource ByteToImage(string fileName, byte[] bytesOfImage, int fileSize)
{
FileStream imageFilest开发者_如何转开发ream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
imageFilestream.Write(bytesOfImage, 0, fileSize);
imageFilestream.Flush();
imageFilestream.Close();
imageFilestream.Dispose();
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(fileName);
myBitmapImage.DecodePixelWidth = 200;
myBitmapImage.EndInit();
return myBitmapImage;
}
When i click ButtonCustomarinfoEdit fist time then it work`s fine. But When i click second time then it throw this exception
Caught: "The process cannot access the file 'C:\20.jpg' because it is being used by another process." (System.IO.IOException)
Exception Message = "The process cannot access the file 'C:\20.jpg' because it is being used by another process.", Exception Type = "System.IO.IOException"Your BitmapImage object keeps the file locked.
Just a small observation, please use the using
statement like this:
using(FileStream imageFilestream = new FileStream(fileName, FileMode.Create, FileAccess.Write)) {
imageFilestream.Write(bytesOfImage, 0, fileSize);
}
otherwise you might run into situations that your file will remain in use (if an exception will occur before you call the Close() method.
精彩评论