System.Windows.Media.Imageing.BItmapImageはこのように使ってファイルを占有しません

1591 ワード

// 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();