使用StretchDIBits复制PNG(带有Alpha)

时间:2019-03-08 18:28:38

标签: c++ windows winapi

有人可以帮我吗?
如何将png复制到虚拟HDC。
透明像素始终为黑色。
我尝试过,但是Alpha总是黑色的。
好像BITMAPINFOHEADER不支持alpha。
非常感谢。
对不起,我的英语。和平。
(请注意,我只需要使用GDI)

// fill background
HBRUSH hbrush = CreateSolidBrush(RGB(color.red(), color.green(), color.blue()));
::FillRect(m_virtualHDC, &wRect, hbrush);
DeleteObject(hbrush);
// load image
QImage pix;
pix.load("D:\\1.png");
// draw image with alpha
BITMAPINFO bmInfo;
ZeroMemory(&bmInfo, sizeof(BITMAPINFO));
BITMAPINFOHEADER& BMPInfoHeader = bmInfo.bmiHeader;
BMPInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
BMPInfoHeader.biWidth = pix.width();
BMPInfoHeader.biHeight = pix.height();
BMPInfoHeader.biPlanes = 1;
BMPInfoHeader.biBitCount = 32;
BMPInfoHeader.biCompression = BI_RGB;
BMPInfoHeader.biSizeImage = pix.byteCount();
BMPInfoHeader.biXPelsPerMeter = 0;
BMPInfoHeader.biYPelsPerMeter = 0;
BMPInfoHeader.biClrUsed = 0;
SetStretchBltMode(m_virtualHDC, HALFTONE);
StretchDIBits(m_virtualHDC,
    targetRect.x(), targetRect.y(), targetRect.width(), targetRect.height(),
    sourceRect.x(), sourceRect.y(), sourceRect.width(), sourceRect.height(),
    pix.bits(), &bmInfo, DIB_RGB_COLORS, SRCCOPY);

1 个答案:

答案 0 :(得分:2)

GDI通常不支持Alpha通道。

您需要使用具有专用alpha支持的API,例如G。 AlphaBlend()GDI+

大多数这些API的重采样质量都很差。我建议使用stb_image_resize之类的第三方库在内存中重新采样,并使用AlphaBlend()之类的API仅将最终结果显示在屏幕上,而无需进一步缩放。