Skip to main content
gamedev.net
🔒 Locked

GetDIBits()

Started by doctorsixstring Jan 8, 2003 at 2:05 PM 5 replies 1.4k views
Original Post
doctorsixstring
doctorsixstring

  
	SelectObject(m_DC, 0);
	void* pImageData = 0;
	BITMAPINFO bmi;
	ZeroMemory(&bmi, sizeof(bmi));
	int result = GetDIBits(m_DC, m_hBitmap, 0, m_ptSize.y, 0, &bmi, DIB_RGB_COLORS);
	GetDIBits(m_DC, m_hBitmap, 0, m_ptSize.y, pImageData, &bmi, DIB_RGB_COLORS);
	SelectObject(m_DC, m_hBitmap);
  
''m_DC'' and ''m_hBitmap'' are two members of my CSurface class. ''m_hBitmap'' is normally selected into m_DC so that I can call BitBlt() and use the bitmap as a source image. ''m_ptSize.y'' is the vertical size of the bitmap and is calculated beforehand. I have been trying to use this code to extract the image data from my HBITMAP object, but to no avail. Calling GetLastError() simply returns "The parameter is incorrect.". Does anyone see anything wrong with what the code? Thanks in advance, Mike
doctorsixstring
doctorsixstring
Apparently the error is in the first call to GetDIBBits(). Its return value is 0, and the ''bmi'' structure is not filled with the bitmap''s data.
doctorsixstring
doctorsixstring
Ah! Thanks AP, it works now. I inserted this line into my code:

bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
doctorsixstring
doctorsixstring
Hmmm... I guess when I said that "it works" I meant that "it compiles". Here is my new code that looks like it should work, but doesn't:


    

void FillDataBuffer()
{
SelectObject(m_DC, 0);

BITMAPINFO bmi;
ZeroMemory(&bmi, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
GetDIBits(m_DC, m_hBitmap, 0, m_ptSize.y, 0, &bmi, DIB_RGB_COLORS);

m_nImageSize = bmi.bmiHeader.biSizeImage;
m_pPtrToBits = new void*[bmi.bmiHeader.biSizeImage];

GetDIBits(m_DC, m_hBitmap, 0, m_ptSize.y, m_pPtrToBits, &bmi, DIB_RGB_COLORS);

SelectObject(m_DC, m_hBitmap);
}

void ClearDataBuffer()
{
if(m_pPtrToBits)
{
delete m_pPtrToBits;
m_pPtrToBits = 0;
}

m_nImageSize = 0;
}


These two functions are members of my CSurface class. This classes' primary members are a handle to a DC (m_DC) and a handle to a bitmap (m_hBitmap). The class is used to easily load bitmap files for usage with BitBlt()'s and whatnot. The two additional members I just added for bit retrieval are 'm_pPtrToBits', which is a void* pointer to the image's bit array, and 'm_nImageSize', which stores the size, in bytes, of the memory allocated by 'm_pPtrToBits'.

Everything works fine, up until the second call to GetDIBits(). At that point, the CSurface object that called FillDataBuffer() is corrupted. This crashes my program and leaves me sobbing on my keyboard.

I have a feeling that I might be overwriting the allocated memory pointed to by m_pPtrToBits, but I can't see how.

Any thoughts? (They would be extremely appeciated)

-Mike

[edited by - doctorsixstring on January 9, 2003 9:58:02 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.