System.Drawing.Bitmap要如何轉換成WPF中可用的ImageSource
来源:互联网 发布:centos 7root密码破解 编辑:程序博客网 时间:2024/05/01 20:15
在一般情況下,如果我們有一些圖片需要顯示在WPF的應用程式中,通常我們會使用Image,以及指定Image.Source屬性,例如說下面這樣
img1.Source = new BitmapImage(new Uri(@"image file path", UriKind.RelativeOrAbsolute));
利用這樣的方式,將圖片檔案顯示在Imagez上面;如果來源是byte array的話,會利用類似這樣的方式
System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); fs.Dispose(); System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = ms; bitmapImage.EndInit(); img1.Source = bitmapImage;這樣就可以由byte陣列轉換成WPF中可以使用的圖片來源了,不過上面這段程式碼有個問題需要處理,在memoryStream的部分,上面並沒有看到Dispose的部分,這樣子不會產生一些記憶體耗用的狀況嗎?於是嘗試加上了MemortStream.Dispose的部分之後發現『疑?阿圖片怎麼顯示不出來了』,這個部分請參考一下
Convert memory stream to BitmapImage?
必須要指定CacheOption的屬性,強制在載入的時候去讀取,才會確保顯示的正常;不然如果需要載入的時候,memorystream已經被釋放掉了,就會造成上面說的圖片無法顯示的狀況了。稍加修改之後,程式碼會像是這樣
System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); fs.Dispose(); System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = ms; bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit(); ms.Dispose(); img1.Source = bitmapImage;
好,看完了一般情況的使用之後,來看一下System.Drawing.Bitmap的部分;會甚麼會有這個需求呢?假設今天你呼叫的是第三方廠商提供的dll,dll回傳的就已經是System.Drawing.Bitmap的話,就會遇到需要轉換的狀況了。那麼應該怎麼轉換呢?下面列出兩種方式作為參考
- 利用上面提到的方式,將Bitmap儲存成memorystream之後,指定給BitmapImage
private BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap) { BitmapImage bitmapImage = new BitmapImage(); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { bitmap.Save(ms, bitmap.RawFormat); bitmapImage.BeginInit(); bitmapImage.StreamSource = ms; bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit(); bitmapImage.Freeze(); } return bitmapImage; }
- 利用System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap方式來做轉換
[System.Runtime.InteropServices.DllImport("gdi32.dll")] public static extern bool DeleteObject(IntPtr hObject); private BitmapSource BitmapToBitmapSource(System.Drawing.Bitmap bitmap) { IntPtr ptr = bitmap.GetHbitmap(); BitmapSource result = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( ptr, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); //release resource DeleteObject(ptr); return result; }
這邊要請特別注意一下,在最後一定要呼叫DeleteObject來做資源的釋放(可以參考MSDN的這篇文章),不然記憶體是會越吃越兇的
- System.Drawing.Bitmap要如何轉換成WPF中可用的ImageSource
- System.Drawing.Bitmap转换成WPF中可用的ImageSource
- bitmap 转 wpf 的 imageSource
- System.Drawing.Bitmap转System.Windows.Media.ImageSource
- WPF(VB.NET) ImageSource 转 System.Drawing.Image
- WPF Image控件中的ImageSource与Bitmap的互相转换
- Wpf ImageSource对象与Bitmap对象的互相转换
- System::Drawing::Bitmap向CBitmap转换的方法
- wpf中imagesource 路径设置问题
- 【C#/WPF】Bitmap、BitmapImage、ImageSource 、byte[]转换问题
- 给图片加水印,System.Drawing,System.Drawing.Bitmap
- 将System.Drawing.Bitmap转换成System.Drawing.Imaging.BitmapSource
- 使用不安全代码将 Bitmap 位图转为 WPF 的 ImageSource 以获得高性能和持续小的内存占用
- WinForm中的Color( System.Drawing.Color)与Wpf中Color(System.Windows.Media.Color)互转
- c#做图像处理的时候需要用到System.Drawing.Bitmap
- wpf System.Drawing.Color转换为System.Windows.Media.Color
- System.Drawing.Color的使用
- WPF开发,Image与ImageSource
- 8086乘法指令MUL,IMUL
- 商业书籍推荐
- log级别设置
- 数据库备份以及链接服务器
- python之matplotlib库的使用
- System.Drawing.Bitmap要如何轉換成WPF中可用的ImageSource
- cannot open output file main: Permission denied问题解决办法
- C. Beautiful Sets of Points
- 沃伦·巴菲特传-一个美国资本家的成长
- eMule电驴eDonkey源代码精辟分析
- 流行的7款jQuery视差和滚动效果的js插件
- wp7访问博客园
- 【问题】SQL Server 2008 安装错误
- HTTP协议详解