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的這篇文章),不然記憶體是會越吃越兇的      
原创粉丝点击