Image控件內(nèi)容保存到本地文件并非占用:
其中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);
從文件轉(zhuǎn)Image控件圖片并非占用:
//方法一:
Stream s = File.Open(fileName, FileMode.Open);
Image img = Bitmap.FromStream(s);
s.Close();
s.Dispose();
//方法二,封裝成函數(shù):
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));
}