P
P
Perzh2014-09-18 13:53:39
C++ / C#
Perzh, 2014-09-18 13:53:39

How to get a screenshot in the right color format?

Hello.
I'm working on a program that captures frames from a region of the screen and generates a GIF animation from them. Here is an example code:

hdcScreen = GetDC(hDesktopWnd);
hdcMemDC = CreateCompatibleDC(hdcScreen);
hbmScreen = CreateCompatibleBitmap(hdcScreen, width, height);
BitBlt( hdcMemDC, 0, 0, width,height, hdcScreen, x, y, SRCCOPY);
GetDIBits(hdcMemDC, hbmScreen, 0,
    (UINT) height, lpbitmap,
    (BITMAPINFO *)&bmpInfo, DIB_RGB_COLORS);
// на данном этапе в lpbitmap хранятся пиксели в RGB формате
// далее пиксели используются для генерации кадра GIF

The problem is that I can get screenshots in 32/24/16 bits per pixel format as RGB color. At the same time, GIF stores pixels as indexes of colors from the palette. And I would like to receive a screenshot immediately in the form of a palette and indexes, so as not to perform unnecessary transformations. So, the question is: how to get a screen immediately with a palette? Or how to get a palette?
PS: I tried to get the palette using GetSystemPaletteEntries, but for some reason the function returns 0 (that is, it doesn't work). The palette is either filled with garbage, or consists of only 5 colors.
PPS: How did I get screenshots with different depths? I just set the flag:
bmpInfo.biBitCount = 32; // Works fine.Each pixel is 32 bit
bmpInfo.biBitCount = 24; // Works fine.Each pixel is 24 bit
bmpInfo.biBitCount = 16; // Works fine.Each pixel is 16 bit
bmpInfo.biBitCount = 8; // Causes Access violation

and received the lpbitmap array in the required format. If you set this flag to 8, then the lpbitmap array is apparently not filled with anything, because I'm catching an access violation when trying to copy pixels into a frame.
Thank you all in advance for your replies, any help and advice is greatly appreciated!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question