Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Ward

Capture screen in OpenGL

This topic is 5277 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

How do I capture the bitmap of the OpenGL window to a HBITMAP? Later on I want to save it in a *.bmp file. Thanks in advance, Ward

Share this post


Link to post
Share on other sites
Advertisement
Guys, don''t bother. After searching this forums I found a solution (I just lost the owner of it).
(But if anyone has a better way to do it, you can always let me know.)

HDC hScreen, hMemDc;
HBITMAP hBitmap;
BITMAPINFO *bmiScreen;
BYTE *bmpBits, *tmpBits;
int nWidth, nHeight;

nWidth = GetSystemMetrics(SM_CXSCREEN);
nHeight = GetSystemMetrics(SM_CYSCREEN);

hScreen = GetDC(NULL);
hMemDc = CreateCompatibleDC(NULL);
hBitmap = CreateCompatibleBitmap(hScreen, nWidth, nHeight);

hBitmap = (HBITMAP)SelectObject(hMemDc, hBitmap);
BitBlt(hMemDc, 0, 0, nWidth, nHeight, hScreen, 0, 0, SRCCOPY);
ReleaseDC(NULL, hScreen);
hBitmap = (HBITMAP)SelectObject(hMemDc, hBitmap);

// hBitmap now contains the bits. You can convert them
// to 24bpp as follows

bmiScreen = (BITMAPINFO *)malloc(sizeof(BITMAPINFO) + sizeof(RGBQUAD) * 256); // allow for 256 colors
memset(bmiScreen, 0, sizeof(BITMAPINFO)); // don''t really care about the color table right now

bmiScreen->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
GetDIBits(aMemDc, hBitmap, 0, nHeight, NULL, bmiScreen, DIB_RGB_COLORS);

// DWORD align the bitmap bits
int nRowSz, nPad;
nRowSz = nWidth * 3;
while (nRowSz & 3) nRowSz++;
nPad = nRowSz - nWidth * 3;

bmpBits = new BYTE[nRowSz * nHeight];
switch (bmiScreen->bmiHeader.biBitCount)
{
case 32:
case 24:
bmiScreen->bmiHeader.biBitCount = 24;
bmiScreen->bmiHeader.biCompression = BI_RGB;
bmiScreen->bmiHeader.biSizeImage = 0;
GetDIBits(aMemDc, hBitmap, 0, nHeight, (LPVOID)bmpBits, bmiScreen, DIB_RGB_COLORS);
break;

case 16:
{
int n16RowSz = width * 2;
while (width & 3) n16RowSz++; // DWORD align

tmpBits = new BYTE[n16RowSz * nHeight];
bmiScreen->bmiHeader.biSizeImage = 0;
bmiScreen->bmiHeader.biCompression = BI_RGB;
GetDIBits(aMemDc, hBitmap, 0, nHeight, (LPVOID)tmpBits, bmiScreen, DIB_RGB_COLORS);

int i, j;
BYTE *ptr = bmpBits;
for (i = 0; i < nHeight; i++)
{
for (j = 0; j < nWidth; j++)
{
WORD data = MAKEWORD(tmpBits[i*n16RowSz+j*2], tmpBits[i*n16RowSz+j*2+1]);
*ptr++ = (BYTE)((data & 0x1F) << 3);
*ptr++ = (BYTE)(((data >> 5) & 0x1F) << 3);
*ptr++ = (BYTE)(((data >> 10) & 0x1F) << 3);
}
for (j = nPad; j--; ) *ptr++ = 0;
}
delete tmpBits;
break;
}

case 8:
{
PALETTEENTRY palSys[256];
int n8RowSz;
GetSystemPaletteEntries(aMemDc, 0, 256, palSys);

n8RowSz = bmiScreen->bmiHeader.biWidth;
while (n8RowSz & 3) n8RowSz++;

bmiScreen->bmiHeader.biCompression = BI_RGB;
bmiScreen->bmiHeader.biClrUsed = bmiScreen->bmiHeader.biClrImportant = 256;

tmpBits = new BYTE[n8RowSz * nWidth];
GetDIBits(aMemDc, hBitmap, 0, nHeight, (LPVOID)tmpBits, bmiScreen, DIB_RGB_COLORS);

int i,j;
BYTE *ptr = bmpBits;
for (i = 0; i < nHeight; i++)
{
for (j = 0; j < nWidth; j++)
{
*ptr++ = palSys[tmpBits[i*n8RowSz+j]].peBlue;
*ptr++ = palSys[tmpBits[i*n8RowSz+j]].peGreen;
*ptr++ = palSys[tmpBits[i*n8RowSz+j]].peRed;
}
for (j = nPad; j--; ) *ptr++ = 0;
}
delete tmpBits;
break;
}

default:
break;
}

DeleteObject(hBitmap);
DeleteDC(hMemDc);

// update our bitmapinfoheader
bmiScreen->bmiHeader.biBitCount = 24;
bmiScreen->bmiHeader.biCompression = BI_RGB;
bmiScreen->bmiHeader.biSizeImage = 0;
bmiScreen->bmiHeader.biClrUsed = bmiScreen->bmiHeader.biClrImportant = 0;


// saving to a file as bitmap

BITMAPFILEHEADER bfhBmp;
memset(&bfhBmp, 0, sizeof(bfhBmp));
bfhBmp.bfType = MAKEWORD(''B'', ''M'');
bfhBmp.bfOffBits = sizeof(bfhBmp) + sizeof(BITMAPINFOHEADER);
bfhBmp.bfSize = bfhBmp.bfOffBits + nRowSz * nHeight;

FILE *fp = fopen("FILENAME", "w+b");
fwrite(&bfhBmp, sizeof(bfhBmp), 1, fp);
fwrite(bmiScreen, sizeof(BITMAPINFOHEADER), 1, fp);
fwrite(bmpBits, 1, nRowSz * nHeight, fp);
fclose(fp);

free(bmiScreen);
delete bmpBits;

/////////////////////////////////////////////////////////

Share this post


Link to post
Share on other sites

  • 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!