user2261597
user2261597

Reputation:

Initialize Gdiplus bitmap with color

I intend to draw in the bitmap pBmp. That part works ok. I want to initialize this bitmap with a (background) color. This is my workaround to accomplish it:

#include <windows.h>
#include <Gdiplus.h>

#define BITMAPX 1000
#define BITMAPY 600

Gdiplus::Bitmap *pBmp;
Gdiplus::Graphics *pGraph;

void init()
{
    pBmp = new Gdiplus::Bitmap(BITMAPX, BITMAPY, PixelFormat24bppRGB);  // Uninitialized bitmap
    pGraph = Gdiplus::Graphics::FromImage(pBmp);                            // Uninitialized graphics object
    pGraph->Clear(Color::Snow);                                 // Set background color and use as a template 
    delete pBmp;                                                // Free for reuse
    pBmp = new Gdiplus::Bitmap(BITMAPX, BITMAPY, pGraph);               // Create bitmap with background color
}

There must be a better way with Palette or the like but after googling for a while I couldn't find a concise example to acheive that. Anyone got a one-liner?

Upvotes: 1

Views: 2305

Answers (1)

Barmak Shemirani
Barmak Shemirani

Reputation: 31599

PixelFormat24bppRGB is 24-bit bitmap with no alpha channel. Use PixelFormat32bppARGB for 32-bit bitmap with alpha channel.

Gdiplus::Bitmap bmp(100, 100, Gdiplus::PixelFormat32bppARGB);

Fill with transparent background:

Gdiplus::Graphics *mem = Gdiplus::Graphics::FromImage(&bmp);
Gdiplus::SolidBrush brush_tr(Gdiplus::Color::Transparent);
mem->FillRectangle(&brush_tr, 0,0,100,100);

This should appear as blank if printed on HDC device context

Gdiplus::Graphics g(hdc);
g.DrawImage(&bmp, 0, 0);

Upvotes: 1

Related Questions