Image控件内容保存到本地文件并非占用:
其中ImageToSave是Wpf中的Image控件:
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)this.ImageToSave.Source));
using (FileStream stream = new FileStream(sfd.FileName, FileMode.Create))
encoder.Save(stream);
从文件转Image控件图片并非占用:
//方法一:
Stream s = File.Open(fileName, FileMode.Open);
Image img = Bitmap.FromStream(s);
s.Close();
s.Dispose();
//方法二,封装成函数:
public static System.Drawing.Image LoadImage(string fileName)
{
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
int byteLength = (int)fileStream.Length;
byte[] fileBytes = new byte[byteLength];
fileStream.Read(fileBytes, 0, byteLength);
fileStream.Close();
return System.Drawing.Image.FromStream(new MemoryStream(fileBytes));
}