Home > Blockchain >  How to get a bitmap from a hdc?
How to get a bitmap from a hdc?

Time:01-27

I can load a hbitmap into a hdc like this:

        Gdiplus::Color Color{ 255, 255, 255 };
        hBitmap = NULL;
        Gdiplus::Bitmap* bitmap = Gdiplus::Bitmap::FromFile(L"home.png", false);
        if (bitmap)
        {
            bitmap->GetHBITMAP(Color, &hBitmap);
            delete bitmap;
        }

        BITMAP bm;
        GetObject(hBitmap, sizeof(bm), &bm);
        HDC hDCMem = CreateCompatibleDC(NULL);
        HBITMAP hBitmapOld = (HBITMAP)SelectObject(hDCMem, hBitmap);

How could I do the reverse, getting back a bitmap loaded into a specific hdc?

I would need first, retrieve the hbitmap and then the bitmap from it? How?

CodePudding user response:

You can use GetCurrentObject() to access the HBITMAP (and HPALETTE) currently selected into an HDC.

Alternatively, you can create a new HBITMAP of desired dimension and color depth, SelectObject() it into a new memory HDC, and then BitBlt()/StretchBlt() the source HDC into it.

Either way, once you have an HBITMAP, you can create a new GDI Bitmap from it using the Bitmap(HBITMAP, HPALETTE) constructor or Bitmap::FromHBITMAP() method.

  •  Tags:  
  • Related