1. 程式人生 > >WPF:How to display a Bitmap on Image control

WPF:How to display a Bitmap on Image control

bug con 另一個 spa and maps api 如果 reat

一個Bitmap文件,叫做screenShotFile, 你可以這樣顯示到Image控件上。

BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(this.screenShotFile, UriKind.Absolute);
bi.EndInit();
this.screenshotImage.Source = bi;

但是有個問題,這個文件你無法刪除,會報錯說“另一個進程正在使用”。什麽鬼?是bug嗎?

如果你的Bitmap還在內存裏,這個問題就比較好解決了:

BitmapSource bs = Imaging.CreateBitmapSourceFromHBitmap(this.bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(bmp.Width, bmp.Height));
this.screenshotImage.Source = bs;

其中,Imaging的namespace是這個:namespace System.Windows.Interop

WPF:How to display a Bitmap on Image control