Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

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);
                Just what I wanted! The reason I didn't try this first what my misconception that FromImage() created a copy while it in fact creates a pointer to the same image memory.
– user2261597
                Jan 14, 2018 at 8:45
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.