找了几天,突然想到答案一直盯着我看!我正在从指向字节数组的指针创建 GDI+ 位图。然后尝试使用相同的指针创建 HBITMAP。但我可以先轻松创建 HBITMAP,然后使用其中的指针创建 GDI+ 位图。
它就像一个魅力!您可以随意混合 GDI 和 GDI+ 操作。该图像同时是普通的 GDI 和 GDI+。您可以从完全相同的像素数据中 BitBlt,而不是使用 DrawImage!
这是代码:
// Create the HBITMAP
BITMAPINFO binfo = new BITMAPINFO();
binfo.biSize = (uint)Marshal.SizeOf(typeof(BITMAPINFO));
binfo.biWidth = width;
binfo.biHeight = height;
binfo.biBitCount = (ushort)Image.GetPixelFormatSize(pixelFormat);
binfo.biPlanes = 1;
binfo.biCompression = 0;
hDC = CreateCompatibleDC(IntPtr.Zero);
IntPtr pointer;
hBitmap = CreateDIBSection(hDC, ref binfo, 0, out pointer, IntPtr.Zero, 0);
// Create the GDI+ bitmap using the pointer returned from CreateDIBSection
gdiBitmap = new Bitmap(width, height, width * binfo.biBitCount >> 3, pixelFormat, pointer);