Winapi GDI+;Windows7上的透明位图
我的应用程序中嵌入了透明PNG。我加载字节,预乘alpha并使用Winapi GDI+;Windows7上的透明位图,winapi,gdi+,gdi,Winapi,Gdi+,Gdi,我的应用程序中嵌入了透明PNG。我加载字节,预乘alpha并使用CreateBitmap创建HBITMAP: HBITMAP hBitmap = CreateBitmap(width, height, 1, 32, bytes); 通常我使用AlphaBlend在应用程序中缩放和绘制这些图像,效果很好。但是,我注意到,在Windows7RDP会话中运行应用程序时,不会绘制任何图像。我想这是因为减少了颜色深度(看起来是16位或24位颜色)。我无法在Windows 10上复制它;因为我似乎无法强制
CreateBitmap
创建HBITMAP
:
HBITMAP hBitmap = CreateBitmap(width, height, 1, 32, bytes);
通常我使用AlphaBlend
在应用程序中缩放和绘制这些图像,效果很好。但是,我注意到,在Windows7RDP会话中运行应用程序时,不会绘制任何图像。我想这是因为减少了颜色深度(看起来是16位或24位颜色)。我无法在Windows 10上复制它;因为我似乎无法强制颜色深度低于32位
我已经读到GDI+是更现代的图形API,我应该尝试使用它而不是常规的GDI。我编写代码如下:
m_hDisplayBitmap = // the earlier created HBITMAP
int srcDim = 64;
int destDim = 32;
// WM_PAINT
// dc = BeginPaint(// etc...)
Graphics g(dc);
BITMAPINFO bmInfo = { 0 };
bmInfo.bmiHeader.biSize = sizeof(bmInfo.bmiHeader);
// Get the BITMAPINFO from the source HBITMAP
GetDIBits(dc, m_hDisplayBitmap, 0, 0, nullptr, &bmInfo, DIB_RGB_COLORS);
bmInfo.bmiHeader.biHeight = -bmInfo.bmiHeader.biHeight;
bmInfo.bmiHeader.biCompression = BI_RGB;
BYTE* lpPixels = new BYTE[bmInfo.bmiHeader.biSizeImage];
// Get the bits from the source HBITMAP
GetDIBits(dc, m_hDisplayBitmap, 0, bmInfo.bmiHeader.biHeight, (LPVOID) lpPixels, &bmInfo, DIB_RGB_COLORS);
// Create a GDI+ bitmap from the source bits
Gdiplus::Bitmap sourceBmp(bmInfo.bmiHeader.biWidth, sourceDim, bmInfo.bmiHeader.biWidth * (bmInfo.bmiHeader.biBitCount / 8), PixelFormat32bppPARGB, lpPixels);
// Create a GDI+ bitmap of the destination dimensions, and draw the source into it
Gdiplus::Bitmap* destBmp = new Gdiplus::Bitmap(destDim, destDim);
Graphics gx(destBmp);
gx.DrawImage(&sourceBmp, 0, 0);
// Create a cached bitmap and draw to the screen
Gdiplus::CachedBitmap m_cachedBitmap = new Gdiplus::CachedBitmap(destBmp, &g);
Gdiplus::Status s = g.DrawCachedBitmap(m_cachedBitmap, 0, 0); // Status is `Ok`
// Cleanup
在Windows 10上,我的位图以预期大小显示,并具有透明度:
在Windows 7上,使用任何显示设置,我都会得到一个灰色斑点:
我做错了什么?您确定
m\u hDisplayBitmap
始终是32位的吗。它是如何创建的?您说您正在使用PNG文件。为什么不使用GDI plus加载PNG文件呢?图像数据是嵌入的,GDI+没有要加载的文件。我可以确认,至少在我问题中的特定示例中,图像数据是32位的。GDI+可以从内存或资源加载图像。当前如何加载PNG图像?使用libpng,然后使用CreateBitmap
获取HBITMAP
使用Gdiplus::Bitmap::FromStream
直接从资源加载位图