GetDIBits help
I'm trying to get the bits of a 1bpp bitmap using getDIBits using this code:
HDC dcmem=NULL;
PBYTE buf=NULL;
LPBITMAPINFO bmpInfo;
HBITMAP bmpfile = NULL;
int dibLineCount;
//load bitmap
bmpfile = (HBITMAP)LoadImageA(NULL, FILENAME, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if(!bmpfile)
{
//Load Image failed
return 0;
}
//select bitmap to dc
dcmem = CreateCompatibleDC ( NULL );
if (NULL==SelectObject(dcmem,bmpfile))
{
//select object failed
DeleteDC(dcmem);
return 0;
}
bmpInfo = (LPBITMAPINFO)calloc(1,sizeof(BITMAPINFO));
bmpInfo->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
//getDIBits to fill bmpInfo
dibLineCount = GetDIBits(dcmem,bmpfile,0,0,NULL,bmpInfo,DIB_RGB_COLORS);
if(dibLineCount == 0)
{
//getdibits 1 failed
DeleteDC(dcmem);
free(bmpInfo);
return 0;
}
if(bmpInfo->bmiHeader.biSizeImage <= 0)
bmpInfo->bmiHeader.biSizeImage=bmpInfo->bmiHeader.biWidth*abs(bmpInfo->bmiHeader.biHeight)*(bmpInfo->bmiHeader.biBitCount+7)/8;
if((buf = (PBYTE)malloc(bmpInfo->bmiHeader.biSizeImage)) == NULL)
return 0;
bmpInfo->bmiHeader.biCompression =BI_RGB;
//get bits
dibLineCount = GetDIBits(dcmem,bmpfile,0,bmpInfo->bmiHeader.biHeight,buf,bmpInfo,DIB_RGB_COLORS);
if(dibLineCount == 0开发者_StackOverflow社区)
{
//getdibits 2 failed
DeleteDC(0,dcmem);
free(buf);
free(bmpInfo);
return 0;
}
Then I send the bits using winsock to another PC. But everytime I send the packet with the bits I see the bits only contain periods "..." or FF in hex which is very weird. I see that the second call to getDIBits returns the correct amount of lines scanned though. Can anyone help me why the bits are like this? Any help would be appreciated.
You're getting the pixel format of the compatible DC instead of the original pixel format, when you first call GetDIBits. Selecting the bitmap into the DC doesn't set up the DC to use the pixel format of the bitmap, it converts the bitmap to the format of the screen. (I suspect the way you're loading the image also converts the bitmap to the pixel format of the screen.)
When you load the bitmap, you probably want to load it as a DIBSECTION by adding LR_CREATEDIBSECTION to the options in LoadImage. This will keep the bits in their original pixel format.
If you want the bits out in a particular pixel format, you should manually initialize the bmpInfo structure to the format you want and then call GetDIBits.
If you want the bits in the pixel format of the original file, you probably don't even need GetDIBits. If you use LR_CREATEDIBSECTION on the LoadImage, you can then use GetObject to get the DIBSECTION, which has the format in it (and probably a pointer to the bits).
精彩评论