wpf 将 IntPtr 转换为位图图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16478449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:45:47  来源:igfitidea点击:

convert IntPtr to bitmapimage

c#wpfimageintptr

提问by Akrem

I use twain_32 for the scan and in twainLib.TransferPicturesuse DibToBitmap.FormHDib(hbitmap)to get a bitmapsourcefrom IntPtrbut I want a bitmapimage.

我用TWAIN_32的扫描和 twainLib.TransferPictures使用DibToBitmap.FormHDib(hbitmap)得到一个bitmapsourceIntPtr,但我想要一个bitmapimage

I want to convert the IntPtrbitmapsourceto bitmapimagedirectly instead to make it a bitmapsourcethen a bitmapimage

我想将转换IntPtrbitmapsourcebitmapimage直接,而不是使之成为bitmapsourcebitmapimage

采纳答案by Chibueze Opata

Use the Bitmap Constructor Method

使用位图构造方法

Bitmap newBitmap = new Bitmap(width, height, Stride, PixelFormat, bitmapSource);

回答by Nicke Manarin

Since you are asking about BitmapSourceand your question is tagged with WPF, here's a way to avoid using System.Drawing.Bitmap:

由于您正在询问BitmapSource并且您的问题带有 标记WPF,因此有一种避免使用的方法System.Drawing.Bitmap

Imaging.CreateBitmapSourceFromHBitmap(imagePointer, IntPtr.Zero, Int32Rect.Empty, 
    BitmapSizeOptions.FromEmptyOptions());

You'll need to use System.Windows.Interop.

你需要使用System.Windows.Interop.