[英]convert IntPtr to bitmapimage
I use twain_32 for the scan and in twainLib.TransferPictures
use DibToBitmap.FormHDib(hbitmap)
to get a bitmapsource
from IntPtr
but I want a bitmapimage
.我使用 twain_32 进行扫描,并在
twainLib.TransferPictures
使用DibToBitmap.FormHDib(hbitmap)
从IntPtr
获取位图bitmapsource
,但我想要一个位图bitmapimage
。
I want to convert the IntPtr
bitmapsource
to bitmapimage
directly instead to make it a bitmapsource
then a bitmapimage
我想将转换
IntPtr
bitmapsource
到bitmapimage
,而不是直接使它成为一个bitmapsource
那么bitmapimage
[DllImport("Kernel32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr GlobalLock(IntPtr hMem);
[DllImport("GdiPlus.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
private static extern int GdipCreateBitmapFromGdiDib(IntPtr pBIH, IntPtr pPix, out IntPtr pBitmap);
public static Bitmap BitmapFromDIB(IntPtr pDIB)
{
IntPtr pPix = (IntPtr)((int)pDIB + 40);
IntPtr pBmp = IntPtr.Zero;
int status = GdipCreateBitmapFromGdiDib(pDIB, pPix, out pBmp);
MethodInfo mi = typeof(Bitmap).GetMethod("FromGDIplus", BindingFlags.Static | BindingFlags.NonPublic);
Bitmap bmp = (Bitmap)mi.Invoke(null, new object[] { pBmp });
if ((status == 0) && (pBmp != IntPtr.Zero)) return bmp;
else return null;
}
// ******
case TwainCommand.TransferReady:
{
ArrayList pics = tw.TransferPictures();
IntPtr img = (IntPtr)pics[i];
PicForm newpic = new PicForm();
var ptrLocked = GlobalLock(img);//lockedptr to image
newpic.pBox.Image = BitmapFromDIB(ptrLocked);
newpic.MdiParent = this;
int picnum = i + 1;
newpic.Text = "Picture " + picnumber.ToString();
newpic.Show();
}
使用位图构造方法
Bitmap newBitmap = new Bitmap(width, height, Stride, PixelFormat, bitmapSource);
Since you are asking about BitmapSource
and 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
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.