下面的代码演示了使用GetDIBits直接读取位图数据。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/iamshuke/archive/2010/07/20/5749948.aspx
下面的代码演示了使用GetDIBits直接读取位图数据。
下面的代码演示了使用GetDIBits直接读取位图数据。
view plaincopy to clipboardprint?
include <math.h>
void CDibtestDlg::OnOK()
{
// TODO: Add extra validation here
HDC hDesktopDC = ::GetDC(NULL);
HDC hTmpDC = CreateCompatibleDC(hDesktopDC);
HBITMAP hBmp = CreateCompatibleBitmap(hDesktopDC, 351, 250); //351x250, 示例数据
SelectObject(hTmpDC, hBmp);
BitBlt(hTmpDC, 0, 0, 351, 250, hDesktopDC, 0, 0, SRCCOPY);
DeleteObject(hTmpDC);
BITMAP bm;
PBITMAPINFO bmpInf;
if(GetObject(hBmp,sizeof(bm),&bm)==0)
{
::ReleaseDC(NULL,hDesktopDC);
return ;
}
int nPaletteSize=0;
if(bm.bmBitsPixel<16)
nPaletteSize=(int)pow(2,bm.bmBitsPixel);
bmpInf=(PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER)+
sizeof(RGBQUAD)*nPaletteSize+(bm.bmWidth+7)/8*bm.bmHeight*bm.bmBitsPixel);
BYTE* buf=((BYTE*)bmpInf) +
sizeof(BITMAPINFOHEADER)+
sizeof(RGBQUAD)*nPaletteSize;
//-----------------------------------------------
bmpInf->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInf->bmiHeader.biWidth = bm.bmWidth;
bmpInf->bmiHeader.biHeight = bm.bmHeight;
bmpInf->bmiHeader.biPlanes = bm.bmPlanes;
bmpInf->bmiHeader.biBitCount = bm.bmBitsPixel;
bmpInf->bmiHeader.biCompression = BI_RGB;
bmpInf->bmiHeader.biSizeImage = (bm.bmWidth+7)/8*bm.bmHeight*bm.bmBitsPixel;
//-----------------------------------------------
if(!::GetDIBits(hDesktopDC,hBmp,0,(UINT)bm.bmHeight,buf,bmpInf,DIB_RGB_COLORS))
{
::ReleaseDC(NULL,hDesktopDC);
LocalFree(bmpInf);
return ;
}
::ReleaseDC(NULL,hDesktopDC);
CString sMsg;
sMsg.Format("BitsPixel:%d,width:%d,height:%d",
bm.bmBitsPixel,bm.bmWidth,bm.bmHeight);
AfxMessageBox(sMsg);
CClientDC dc(this);
int nOffset;
BYTE r,g,b;
int nWidth = bm.bmWidth*bm.bmBitsPixel/8;
nWidth = ((nWidth+3)/4)*4; //4字节对齐
if(bmpInf->bmiHeader.biBitCount == 8)
{
for(int i=0; i<bm.bmHeight; i++)
{
for(int j=0; j<bm.bmWidth; j++)
{
RGBQUAD rgbQ;
rgbQ = bmpInf->bmiColors[buf[i*nWidth+j]];
dc.SetPixel(j,bm.bmHeight-i,RGB(rgbQ.rgbRed,rgbQ.rgbGreen,rgbQ.rgbBlue)); //测试显示
}
}
}
else if(bmpInf->bmiHeader.biBitCount == 16)
{
for(int i=0; i<bm.bmHeight; i++)
{
nOffset = i*nWidth;
for(int j=0; j<bm.bmWidth; j++)
{
b = buf[nOffset+j*2]&0x1F;
g = buf[nOffset+j*2]>>5;
g |= (buf[nOffset+j*2+1]&0x03)<<3;
r = (buf[nOffset+j*2+1]>>2)&0x1F;
r *= 8;
b *= 8;
g *= 8;
dc.SetPixel(j, bm.bmHeight-i, RGB(r,g,b)); //测试显示
}
}
}
else if(bmpInf->bmiHeader.biBitCount == 24)
{
for(int i=0; i<bm.bmHeight; i++)
{
nOffset = i*nWidth;
for(int j=0; j<bm.bmWidth; j++)
{
b = buf[nOffset+j*3];
g = buf[nOffset+j*3+1];
r = buf[nOffset+j*3+2];
dc.SetPixel(j, bm.bmHeight-i, RGB(r,g,b)); //测试显示
}
}
}
else if(bmpInf->bmiHeader.biBitCount == 32)
{
for(int i=0; i<bm.bmHeight; i++)
{
nOffset = i*nWidth;
for(int j=0; j<bm.bmWidth; j++)
{
b = buf[nOffset+j*4];
g = buf[nOffset+j*4+1];
r = buf[nOffset+j*4+2];
dc.SetPixel(j, bm.bmHeight-i, RGB(r,g,b)); //测试显示
}
}
}
DeleteObject(hBmp);
LocalFree(bmpInf);
//CDialog::OnOK();
}