【问题标题】:Using StretchDIBits to copy PNG (with alpha)使用 StretchDIBits 复制 PNG(带 alpha)
【发布时间】:2019-03-09 04:09:43
【问题描述】:

有人可以帮我吗?
如何将 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);

【问题讨论】:

    标签: c++ windows winapi


    【解决方案1】:

    GDI 通常不支持 Alpha 通道。

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

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

    【讨论】:

    • 您好,已经尝试使用 AlphaBlend(),但似乎它会更改所有像素的 Alpha 通道。
    • @Ruslan 一定要使用premultiplied alpha,否则会得到错误的结果。默认情况下不预乘 PNG。
    猜你喜欢
    • 2011-02-05
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多