Reputation: 4652
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
.
I want to convert the IntPtr
bitmapsource
to bitmapimage
directly instead to make it a bitmapsource
then a bitmapimage
Upvotes: 1
Views: 9758
Reputation: 435
[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();
}
Upvotes: 1
Reputation: 3360
Since you are asking about BitmapSource
and your question is tagged with WPF
, here's a way to avoid using System.Drawing.Bitmap
:
Imaging.CreateBitmapSourceFromHBitmap(imagePointer, IntPtr.Zero, Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
You'll need to use System.Windows.Interop
.
Upvotes: 0
Reputation: 10044
Use the Bitmap Constructor Method
Bitmap newBitmap = new Bitmap(width, height, Stride, PixelFormat, bitmapSource);
Upvotes: 0