WPF:How to display a Bitmap on Image control

一个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

 

posted @ 2017-07-18 19:09  UniversalAIPlatform  阅读(696)  评论(1编辑  收藏  举报