CreateBitmapImageWPF我有一个ushort[],其中包含我需要在屏幕上显示的图像数据,当我创建一个Windows.System.Drawing.Bitmap,并将其转换为BitmapImage有时,这感觉像是一种缓慢且无用的方法。有没有人有最快的方法来创建ushort[]的BitmapImage?或者另外从数据创建一个ImageSource对象?谢谢,Eamon我之前将Bitmap转换为BitmapImage的方法是:MemoryStreamms=newMemoryStream();位图.Save(ms,ImageFormat.Png);ms.Position=0;BitmapImagebi=newBitmapImage();双.BeginInit();bi.StreamSource=ms;bi.EndInit();我能够使用Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(),IntPtr.Zero,Int32Rect.Empty,System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());加快速度编辑:任何使用它的人都应该知道bitmap.GetHbitmap创建一个非托管对象,因为这是非托管的,它不会被.net垃圾收集器拾取并且必须删除以避免内存泄漏,使用以下代码来修复这个:[System.Runtime.InteropServices.DllImport("gdi32.dll")]publicstaticexternboolDeleteObject(IntPtrhObject);IntPtrhBitmap=bitmap.GetHbitmap();尝试{imageSource=Imaging.CreateBitmapSourceFromHBitmap(hBitmap,IntPtr.Zero,Int32Rect.Empty,System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());}catch(Exceptione){}最后{DeleteObject(hBitmap);}(它不是很整洁,必须像这样导入一个DLL,但这是来自msdn,似乎是解决这个问题的唯一办法-http://msdn.microsoft.com/en-us/library/1dz311e4.aspx)以上就是C#学习教程:CreateBitmapImageWPFShare的全部内容,如果对你有用,需要了解更多C#学习教程,希望大家多多关注——本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
