System.Windows.Media.Imageing.BItmapImage 这么用才不会占用文件

时间:2024-07-22 15:03:26
// Read byte[] from png file
BinaryReader binReader = new BinaryReader(File.Open(filepath, FileMode.Open));
FileInfo fileInfo = new FileInfo(filepath);
byte[] bytes = binReader.ReadBytes((int)fileInfo.Length);
binReader.Close(); // Init bitmap
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
//bitmap.DecodePixelWidth = Convert.ToInt32(w);
//bitmap.DecodePixelHeight = Convert.ToInt32(h);
//bitmap.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
//bitmap.CreateOptions = System.Windows.Media.Imaging.BitmapCreateOptions.IgnoreImageCache;
bitmap.StreamSource = new MemoryStream(bytes);
bitmap.EndInit();