Question opengl windows to bmp file

Started by
14 comments, last by ioda 19 years, 2 months ago
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
Advertisement
true link : http://www.codeguru.com/Cpp/G-M/opengl/article.php/c2711

sorry
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]
ok thx a lot, i'm gonna try this now, i will tell you if it works like this
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;
}
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
ok thx it would be great

i'm going to sleep on this.
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();
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.
nobody know how to do?

i retry this morning but still not find

This topic is closed to new replies.

Advertisement