PDA

Click to See Complete Forum and Search --> : DIB sections


CornedBee
Jun 4th, 2002, 07:20 AM
Why does this code produce an access violation at the CopyMemory line?

bool CDbBitmap::BmpToDibSec(BYTE *pData)
{
PBITMAPFILEHEADER pBMFH = (PBITMAPFILEHEADER)pData;
ASSERT(pBMFH->bfType == 0x4D42); // check again if really BMP

DWORD dwBitOffset = pBMFH->bfOffBits;
BITMAPINFO *pbi = (BITMAPINFO*)(pData + sizeof(BITMAPFILEHEADER));
// Create the DIB section
BYTE *pBits;
HBITMAP hB = ::CreateDIBSection(NULL, pbi, DIB_RGB_COLORS, (PVOID*)&pBits, NULL, 0);
::CopyMemory(pBits, pData + dwBitOffset, pBMFH->bfSize - dwBitOffset);
Attach((HGDIOBJ)hB);
return true;
}

pData is a pointer to memory which contains a complete BMP file. CreateDIBSection should allocate memory and set pBits to point to this memory.
I tried using HeapSize to get the block size of pBits but since it is allocated by the system it fails.

jim mcnamara
Jun 4th, 2002, 09:28 AM
You cannot call ::CreateDIBSection(NULL....);
Rather than NULL it has to have a valid DC handle--usually from CreateCompatibleDC.

If you can do it that way, it's news to me, anyway. :D
CreateDIBSection is failing. No memory allocated.

CornedBee
Jun 4th, 2002, 10:34 AM
As described in Petzold's Programming Windows I need the dc handle only if I use DIB_PAL_COLORS.
Also, the function returns a bitmap handle (not NULL) and pBits isn't NUL either. (Both would be NULL if the function failed).

There are some news for you :D
I know that there is memory, I just think it's not enough.