Jump to content
  • Advertisement
Sign in to follow this  

GDI Bitmap color conversion

This topic is 2564 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i need a little help with some code in my project, i've been using this code below to generate a screenshot, store it into some buffer, then converting it to 24 bits using my own conversion routine.

HWND hDesktopWnd = GetDesktopWindow();
HDC hdc = GetDC(hDesktopWnd);

SShot s;
s.x = 0;
s.y = 0;
s.w = GetSystemMetrics(SM_CXSCREEN);
s.h = GetSystemMetrics(SM_CYSCREEN);

s.hdc = CreateCompatibleDC(hdc);
s.hbmp = CreateCompatibleBitmap(hdc,s.w,s.h);
s.ex_hbmp = (HBITMAP)SelectObject(s.hdc, s.hbmp);




// Allocate buffers
DWORD NumPixels = s.w * s.h;
Buffers.ScreenShot.Allocate(NumPixels * 4);
Buffers.Converted.Allocate(NumPixels * 3);

// Fill the ScreenShot buffer
GetBitmapBits(s.hbmp, Buffers.ScreenShot.GetBufferSize(), Buffers.ScreenShot.GetBuffer());

// Convert it to 24 bits in another buffer
DWORD Ret = ConvertTo24bits(Buffers.Converted.GetBuffer(), Buffers.ScreenShot.GetBuffer(), Buffers.ScreenShot.GetBufferSize());


// Release the bitmap handles
if(SelectObject(s.hdc, s.hbmp)){

The problem is, if the screen resolution use another color format than 32 bits, this code won't work.
So, i would like to know how can i convert the image to 24 bits from any pixel format the desktop is currently using.


Share this post

Link to post
Share on other sites
The simplest solution would be to create another bitmap in memory, this time with 24 bits. Then you select each into their own HDC and BitBlt the graphic from one to the other. Let Windows take care of the conversion.

Share this post

Link to post
Share on other sites
After some experimentation and some explanation about GetDIBits, all i had to do was to replace the GetBitmapBits call by this, getting rid of my own format routine at the same time:

LPBITMAPINFO lpbi = (LPBITMAPINFO)(new char[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)]);
ZeroMemory(lpbi, sizeof(BITMAPINFO));
int x = sizeof(BITMAPINFOHEADER);
int y = sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);
lpbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);

SelectObject(s.hdc, s.ex_hbmp);

GetDIBits(s.hdc, s.hbmp, 0, s.h, NULL, lpbi, DIB_RGB_COLORS);
lpbi->bmiHeader.biBitCount = 16;
lpbi->bmiHeader.biSizeImage = (s.w * s.h) * (lpbi->bmiHeader.biBitCount / 8);
lpbi->bmiHeader.biCompression = BI_RGB;

LPVOID lpvBits = new char[lpbi->bmiHeader.biSizeImage];
ZeroMemory(lpvBits, lpbi->bmiHeader.biSizeImage);

GetDIBits(s.hdc, s.hbmp, 0, s.h, lpvBits, lpbi, DIB_RGB_COLORS);
SelectObject(s.hdc, s.hbmp);


Just posting in case this can help someone else...

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!