Jump to content
  • Advertisement
Sign in to follow this  
ioda

OpenGL Question opengl windows to bmp file

This topic is 5002 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've read this article about copying a bmp (from an opengl windows) to the clipboard and it work but i would like to copy directly to a .bmp file. it's written that it's done in a few lines but didn't reach to do it. if someone could tell me which line remove and which line add, it would be great article : http://www.codeguru.com/Cpp/G-M/ope...ticle.php/c2711 Thank you in advance

Share this post


Link to post
Share on other sites
Advertisement
I think this is what you need. Since it is already saved in that first part you will use this to save it -

// Create logical palette if device support a palette
CPalette pal;
if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE )
{
UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);
LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
pLP->palVersion = 0x300;

pLP->palNumEntries =
GetSystemPaletteEntries( dc, 0, 255, pLP->palPalEntry );

// Create the palette
pal.CreatePalette( pLP );

delete[] pLP;
}

// Convert the bitmap to a DIB
HANDLE hDIB = DDBToDIB( bitmap, BI_RGB, &pal );

if( hDIB == NULL )
return FALSE;

// Write it to file
WriteDIB( szFile, hDIB );

// Free the memory allocated by DDBToDIB for the DIB
GlobalFree( hDIB );
return TRUE;




I think you will replace this block of code with the previous -

// Generate handle
HANDLE handle = (HANDLE)::GlobalAlloc (GHND,sizeof(BITMAPINFOHEADER) + NbBytes);
if(handle != NULL)
{
// Lock handle
char *pData = (char *) ::GlobalLock((HGLOBAL)handle);
// Copy header and data
memcpy(pData,&header,sizeof(BITMAPINFOHEADER));
memcpy(pData+sizeof(BITMAPINFOHEADER),pPixelData,NbBytes);
// Unlock
::GlobalUnlock((HGLOBAL)handle);

// Push DIB in clipboard
OpenClipboard();
EmptyClipboard();
SetClipboardData(CF_DIB,handle);
CloseClipboard();
}



I think that's about it. Hope it helps.

- Drew

[edit] The missing function you need are on this page and this page



[Edited by - Drew_Benton on February 10, 2005 6:49:24 PM]

Share this post


Link to post
Share on other sites
here is a part of my function, i had to put MemDC in the place of dc, now i have a result but the image is all black, maybe a little change to do, i will search

thx for your solution, if you see what i have to corect... :-)


// Get client geometry
CRect rect;
GetClientRect(&rect);
CSize size(rect.Width(),rect.Height());

// Lines have to be 32 bytes aligned, suppose 24 bits per pixel
// I just cropped it
size.cx -= size.cx % 4;

// Create a bitmap and select it in the device context
// Note that this will never be used ;-) but no matter
CBitmap bitmap;
CDC *pDC = GetDC();
CDC MemDC;
ASSERT(MemDC.CreateCompatibleDC(NULL));
ASSERT(bitmap.CreateCompatibleBitmap(pDC,size.cx,size.cy));
MemDC.SelectObject(&bitmap);

// Alloc pixel bytes
int NbBytes = 3 * size.cx * size.cy;
unsigned char *pPixelData = new unsigned char[NbBytes];

// Copy from OpenGL
::glReadPixels(0,0,size.cx,size.cy,GL_BGR_EXT,GL_UNSIGNED_BYTE,pPixelData); // mis GL_BRG a la place de GL_RGB car inverse ds opengl

// Fill header
BITMAPINFOHEADER header;
header.biWidth = size.cx;
header.biHeight = size.cy;
header.biSizeImage = NbBytes;
header.biSize = 40;
header.biPlanes = 1;
header.biBitCount = 3 * 8; // RGB
header.biCompression = 0;
header.biXPelsPerMeter = 0;
header.biYPelsPerMeter = 0;
header.biClrUsed = 0;
header.biClrImportant = 0;

// Create logical palette if device support a palette
CPalette pal;
// CWindowDC dc= GetDC();

if( MemDC.GetDeviceCaps(RASTERCAPS) & RC_PALETTE )
{
UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);
LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
pLP->palVersion = 0x300;

pLP->palNumEntries =
GetSystemPaletteEntries( MemDC, 0, 255, pLP->palPalEntry );

// Create the palette
pal.CreatePalette( pLP );

delete[] pLP;
}

Share this post


Link to post
Share on other sites
Yea I tried it as well and got all black. I think it has to do with something in the way the data is being saved. I will let you know if I can find a solution as well.

- Drew

Share this post


Link to post
Share on other sites
Do we need really the function DDBToDIB ?

there is no simple thing to do to replace these lines which put all in the clipboard (for putting in a file instead?):

// Push DIB in clipboard
OpenClipboard();
EmptyClipboard();
SetClipboardData(CF_DIB,handle);
CloseClipboard();

Share this post


Link to post
Share on other sites
Quote:
Original post by ioda
Do we need really the function DDBToDIB ?

there is no simple thing to do to replace these lines which put all in the clipboard (for putting in a file instead?):

// Push DIB in clipboard
OpenClipboard();
EmptyClipboard();
SetClipboardData(CF_DIB,handle);
CloseClipboard();


I think there is but I do not know what :(. I was looking to see how to find to save the clipboard into a bmp, but thats not going so well, but you are on the right track.

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!