1

我使用 twain_32 进行扫描并 twainLib.TransferPictures用于DibToBitmap.FormHDib(hbitmap)获取bitmapsourcefromIntPtr但我想要一个bitmapimage.

我想直接将其转换IntPtr bitmapsourcebitmapimagebitmapsourcebitmapimage

4

3 回答 3

1
      [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();
    }
于 2020-06-05T10:05:33.940 回答
0

使用位图构造方法

Bitmap newBitmap = new Bitmap(width, height, Stride, PixelFormat, bitmapSource);
于 2013-05-10T09:00:12.250 回答
0

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

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

您将需要使用System.Windows.Interop.

于 2016-07-29T23:16:05.010 回答