是否有可能直接从GDI +位图BitBlt?

jnm*_*nm2 5 winapi gdi+ gdi bitblt

是否可以使用BitBlt直接从GDI +位图复制而不使用GetHBitmap?

GetHBitmap很慢,因为它创建了整个图像的新副本,除了比BitBlt副本慢,并且必须处理给定的HBITMAP.图像很大.

有没有办法指向BitBlt使用原始GDI +图像的像素数据?

编辑: 我可以获得指向GDI +位图像素数据在内存中的位置的指针.我可以创建一个指向GDI +位图像素数据的HBITMAP以避免额外的复制,并从中创建BitBlt吗?

jnm*_*nm2 6

搜索了几天之后,它突然袭击了我,答案一直盯着我的脸!我是从指向字节数组的指针创建GDI +位图.然后尝试使用相同的指针创建HBITMAP.但我可以先轻松创建HBITMAP并使用指针来创建GDI +位图.

它就像一个魅力!您可以随意混合使用GDI和GDI +操作.图像既是普通的GDI,也是GDI +.而不是使用DrawImage,你可以从完全相同的像素数据BitBlt!

这是代码:

// 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);
Run Code Online (Sandbox Code Playgroud)