System.Drawing.Bitmap转换成WPF中可用的ImageSource

12 篇文章 0 订阅
在一般情況下,如果我們有一些圖片需要顯示在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的這篇文章),不然記憶體是會越吃越兇的
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值