-3

i'm sorry for what i did. i edited.

i'd like to use Fillrect on 32bit HBITMAP which is Created with CreateDIBSection

but i can't make rect visible in color that i want to. (i Drawed a fillrect with CreateSolidBrush blue(RGB(0, 0, 255)) on 32bit HBITMAP(hdcbmp), but it doesn't appear blue.)

here is source code

is there anyway to show rect color that i want to?

sorry for my poor english.

void DrawAlphaBitmap(HWND hWnd, ULONG uWidth, ULONG uHeight)
{
    BLENDFUNCTION  bf;
    HBITMAP        hbitmap;
    HBITMAP        hOldBitmap;
    BITMAPINFO     bmi;
    PVOID          pvBits;
    HDC            hdcwnd = GetDC(hWnd);
    HDC            hdcbmp = CreateCompatibleDC(hdcwnd);
    ZeroMemory(&bmi, sizeof(BITMAPINFO));
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biWidth = uWidth;
    bmi.bmiHeader.biHeight = uHeight;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biCompression = BI_RGB;
    bmi.bmiHeader.biSizeImage = bmi.bmiHeader.biWidth *     bmi.bmiHeader.biHeight * 4;

    hbitmap = CreateDIBSection(hdcbmp, &bmi, DIB_RGB_COLORS, &pvBits, NULL, 0x0);

    hOldBitmap = (HBITMAP)SelectObject(hdcbmp, hbitmap);
    bf.BlendOp = AC_SRC_OVER;
    bf.BlendFlags = 0;
    bf.SourceConstantAlpha = 0xff;
    bf.AlphaFormat = AC_SRC_ALPHA;

    RECT rc2 = { 100, 100, 200, 200 };
    FillRect(hdcbmp, &rc2, CreateSolidBrush(RGB(0, 0, 255)));

    AlphaBlend(hdcwnd, 0, 0, uWidth, uHeight, hdcbmp, 0, 0, uWidth, uHeight, bf);
    SelectObject(hdcbmp, hOldBitmap);
    DeleteObject(hbitmap);
    DeleteDC(hdcbmp);
    ReleaseDC(hWnd, hdcwnd);
}
  • Please don't post picture of your code. Use copy/paste to show the code in text format. – Barmak Shemirani Oct 06 '18 at 03:33
  • [Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question) – Rafael Oct 06 '18 at 05:04
  • "CreateSolidBrush doesn't work" is not enough information to go on. – Jonathan Potter Oct 06 '18 at 06:05
  • sorry i changed to text. – Myung Yoon Oct 06 '18 at 10:05
  • GDI APIs like `FillRect` don't set alpha channel. Use newer API like [GDI+](https://learn.microsoft.com/en-us/windows/desktop/gdiplus/-gdiplus-gdi-start) or [Direct2D](https://learn.microsoft.com/en-us/windows/desktop/direct2d/direct2d-portal) for proper handling of alpha channel. – zett42 Oct 06 '18 at 21:45

1 Answers1

3

From documentation for BLENDFUNCTION:

AlphaFormat:
This flag is set when the bitmap has an Alpha channel (that is, per-pixel alpha).

In this case, alpha channel is not set. CreateDIBSection initializes the alpha values to zero. When AC_SRC_ALPHA is set, AlphaBlend ignores pixels whose alpha value is zero. Modify your code as follows:

//bf.AlphaFormat = AC_SRC_ALPHA; <- remove   
bf.AlphaFormat = 0; //replace with 0


Side note, you have resource leak in creation of HBRUSH handle. Change the code to
HBRUSH hbrush = CreateSolidBrush(RGB(0, 0, 255));
RECT rc2 = { 0, 0, w, h };
FillRect(memdc, &rc2, hbrush);
DeleteObject(hbrush);

Ideally, your function prototype should be void DrawAlphaBitmap(HDC hdc, ULONG uWidth, ULONG uHeight); so that HDC can be passed directly, for example from BeginPaint/EndPaint in WM_PAINT message.

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • thank you for your comment. but if i replace AlphaFormat to 0, background of bitmap becomes black so i can't make transparent background. then is there anyway to make background transparent when use 32bit HBITMAP? without using Gdiplus::Bitmap or Gdiplus::Metafile. – Myung Yoon Oct 07 '18 at 00:15
  • 1
    You are asking a different question now. – Barmak Shemirani Oct 07 '18 at 00:49