无法从 GDI 获取的 HBITMAP 创建单色位图位图::GetHBITMAP

发布于 2024-10-10 03:07:46 字数 606 浏览 2 评论 0原文

我无法使用 SetBkColor() > 创建 24BPP 彩色图像的单色蒙版BitBlt[SRCCOPY]。面具最终变成完全黑色。仅当我使用 LoadImage() 来获取 HBITMAP 时,整个事情才有效。

Bitmap img(L"Ball.bmp");
HBITMAP hBM;
img.GetHBITMAP(Color::White, &hBM);
//hBM = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BALL));
.
.
SelectObject(hDCSrc, hBM);
SetBkColor(RGB(0xFF, 0xFF, oxFF));
BitBlt(hDCMem, 0, 0, img.GetWidth(), img.GetHeight(), hDCSrc, 0, 0, SRCCOPY);

//hDCMem is copletely black; but OK when using LoadImage() instead

其他有同样问题的人建议使用 Graphics::GetHDC 并使用此 DC 执行所需操作作为解决方法。但为什么它没有发挥应有的作用。

即使这个解决方法也不起作用。请帮忙:(

I am unable to Create a Monochrome Mask for a 24BPP Colour image with SetBkColor() > BitBlt[SRCCOPY]. The Mask ends up completely Black. The entire thing works only if I use LoadImage() instead to get the HBITMAP.

Bitmap img(L"Ball.bmp");
HBITMAP hBM;
img.GetHBITMAP(Color::White, &hBM);
//hBM = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BALL));
.
.
SelectObject(hDCSrc, hBM);
SetBkColor(RGB(0xFF, 0xFF, oxFF));
BitBlt(hDCMem, 0, 0, img.GetWidth(), img.GetHeight(), hDCSrc, 0, 0, SRCCOPY);

//hDCMem is copletely black; but OK when using LoadImage() instead

Other people with the same problem have suggested using Graphics::GetHDC and doing the required with this DC as a workaround. But why does it not work as it should.

Even this workaround din' work. Please help :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦巷 2024-10-17 03:07:46

是的,您可以仅使用 GDI 调用从 24bpp 位图构建单色蒙版,而无需扫描各个像素,但您必须决定将 24bpp 位图中的哪种颜色视为透明。请参阅下面的代码:

HBITMAP Bitmap2Mask(HBITMAP hBm, COLORREF TransparentColor)
{
    HBITMAP hBmpMask = 0;
    BITMAP bmp;
    GetObject(hBm, sizeof(BITMAP), &bmp);

    if (bmp.bmBitsPixel == 24) //The question was about 24bpp bitmaps (without an Alpha channel)
    {
        HDC hDCsrc = CreateCompatibleDC(0);
        HBITMAP hBmpOldSrc = (HBITMAP)SelectObject(hDCsrc, hBm);
        
        HDC hDCdest = CreateCompatibleDC(0);
        hBmpMask = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
        HBITMAP hBmpOldDest = SelectObject(hDCdest, hBmpMask);        
        SetBkColor(hDCsrc, TransparentColor);
        BitBlt(hDCdest, 0, 0, bmp.bmWidth, bmp.bmHeight, hDCsrc, 0, 0, SRCCOPY); //Everything with the TransparentColor ends up white in the mask (meaning "transparent") while everythig else ends up black (meaning "opaque").
        hBmpMask = (HBITMAP)SelectObject(hDCdest, hBmpOldDest);  //Push out hBmpMask
        SelectObject(hDCsrc, hBmpOldSrc); //Push out the hBm
        DeleteDC(hDCdest);
        DeleteDC(hDCsrc);
    }

    return hBmpMask;  //return the Monochrome mask
}

您还可以使用 TransparentBlt() 函数来完成此操作。

Yes, you can construct a monochrome mask out of an 24bpp bitmap with GDI calls only and without scanning the individual pixels, but you have to decide which color in the 24bpp bitmap you want to treat as transparent. See the code below:

HBITMAP Bitmap2Mask(HBITMAP hBm, COLORREF TransparentColor)
{
    HBITMAP hBmpMask = 0;
    BITMAP bmp;
    GetObject(hBm, sizeof(BITMAP), &bmp);

    if (bmp.bmBitsPixel == 24) //The question was about 24bpp bitmaps (without an Alpha channel)
    {
        HDC hDCsrc = CreateCompatibleDC(0);
        HBITMAP hBmpOldSrc = (HBITMAP)SelectObject(hDCsrc, hBm);
        
        HDC hDCdest = CreateCompatibleDC(0);
        hBmpMask = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
        HBITMAP hBmpOldDest = SelectObject(hDCdest, hBmpMask);        
        SetBkColor(hDCsrc, TransparentColor);
        BitBlt(hDCdest, 0, 0, bmp.bmWidth, bmp.bmHeight, hDCsrc, 0, 0, SRCCOPY); //Everything with the TransparentColor ends up white in the mask (meaning "transparent") while everythig else ends up black (meaning "opaque").
        hBmpMask = (HBITMAP)SelectObject(hDCdest, hBmpOldDest);  //Push out hBmpMask
        SelectObject(hDCsrc, hBmpOldSrc); //Push out the hBm
        DeleteDC(hDCdest);
        DeleteDC(hDCsrc);
    }

    return hBmpMask;  //return the Monochrome mask
}

You could also accomplish this with the TransparentBlt() function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文