int c;
cin>>c;
cout<<endl;
HDC hDesktopDC = ::GetDC(NULL);
HDC hTmpDC = CreateCompatibleDC(hDesktopDC);
HBITMAP hBmp = CreateCompatibleBitmap(hDesktopDC, 140, 140); //500x500, 示例数据
SelectObject(hTmpDC, hBmp);
BitBlt(hTmpDC, 770, 400, 140, 140, hDesktopDC, 0, 0, SRCCOPY);
DeleteObject(hTmpDC);
BITMAP bm;
PBITMAPINFO bmpInf;
if(GetObject(hBmp,sizeof(bm),&bm)==0)
{
::ReleaseDC(NULL,hDesktopDC);
return -1;
}
int nPaletteSize=0;
if(bm.bmBitsPixel<16)
nPaletteSize=(int)pow((double)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;
//-----------------------------------------------
//memset(bmpInf->bmiHeader, 0, sizeof(BITMAPINFOHEADER));
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;
/*
BITMAPINFO infoHeader;
memset(&infoHeader.bmiHeader, 0, sizeof(BITMAPINFOHEADER));
infoHeader.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
infoHeader.bmiHeader.biWidth = w;
infoHeader.bmiHeader.biHeight = h;
infoHeader.bmiHeader.biPlanes = 1;
infoHeader.bmiHeader.biBitCount = 32; // 这里建议用32,经过测试在我的机器上比24速度快比较多
infoHeader.bmiHeader.biCompression = BI_RGB;
*/
//-----------------------------------------------
cout<<"buf size 1 = "<<sizeof(buf)<<endl;
if(!::GetDIBits(hDesktopDC,hBmp,0,(UINT)bm.bmHeight,buf,bmpInf,DIB_RGB_COLORS))
//GetDIBits(hcDC,hBitmap,0,dwY,lpBuf,&b,DIB_RGB_COLORS);
{
::ReleaseDC(NULL,hDesktopDC);
LocalFree(bmpInf);
return -2;
}
cout<<"buf size 2 = "<<sizeof(buf)<<endl;
::ReleaseDC(NULL,hDesktopDC);
CString sMsg;
sMsg.Format(_T("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)
{
cout<<"1111111111111111111111111111111111111"<<endl;
Sleep(1000);
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)); //测试显示
cout<<(int)rgbQ.rgbRed<<","<<(int)rgbQ.rgbGreen<<","<<(int)rgbQ.rgbBlue<<"\t";
}
cout<<endl;
}
}
else if(bmpInf->bmiHeader.biBitCount == 16)
{
cout<<"22222222222222222222222222222222222222"<<endl;
Sleep(1000);
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)); //测试显示
cout<<(int)r<<","<<(int)g<<","<<(int)b<<"\t";
}
cout<<endl;
}
}
else if(bmpInf->bmiHeader.biBitCount == 24)
{
cout<<"333333333333333333333333333333333333333333333"<<endl;
Sleep(1000);
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)); //测试显示
cout<<(int)r<<","<<(int)g<<","<<(int)b<<"\t";
}
cout<<endl;
}
}
else if(bmpInf->bmiHeader.biBitCount == 32)
{
cout<<"44444444444444444444444444444444444444444444444444"<<endl;
Sleep(1000);
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)); //测试显示
cout<<(int)r<<","<<(int)g<<","<<(int)b<<"\t";
}
cout<<endl;
}
}
DeleteObject(hBmp);
LocalFree(bmpInf);
return 0;
cin>>c;
cout<<endl;
HDC hDesktopDC = ::GetDC(NULL);
HDC hTmpDC = CreateCompatibleDC(hDesktopDC);
HBITMAP hBmp = CreateCompatibleBitmap(hDesktopDC, 140, 140); //500x500, 示例数据
SelectObject(hTmpDC, hBmp);
BitBlt(hTmpDC, 770, 400, 140, 140, hDesktopDC, 0, 0, SRCCOPY);
DeleteObject(hTmpDC);
BITMAP bm;
PBITMAPINFO bmpInf;
if(GetObject(hBmp,sizeof(bm),&bm)==0)
{
::ReleaseDC(NULL,hDesktopDC);
return -1;
}
int nPaletteSize=0;
if(bm.bmBitsPixel<16)
nPaletteSize=(int)pow((double)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;
//-----------------------------------------------
//memset(bmpInf->bmiHeader, 0, sizeof(BITMAPINFOHEADER));
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;
/*
BITMAPINFO infoHeader;
memset(&infoHeader.bmiHeader, 0, sizeof(BITMAPINFOHEADER));
infoHeader.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
infoHeader.bmiHeader.biWidth = w;
infoHeader.bmiHeader.biHeight = h;
infoHeader.bmiHeader.biPlanes = 1;
infoHeader.bmiHeader.biBitCount = 32; // 这里建议用32,经过测试在我的机器上比24速度快比较多
infoHeader.bmiHeader.biCompression = BI_RGB;
*/
//-----------------------------------------------
cout<<"buf size 1 = "<<sizeof(buf)<<endl;
if(!::GetDIBits(hDesktopDC,hBmp,0,(UINT)bm.bmHeight,buf,bmpInf,DIB_RGB_COLORS))
//GetDIBits(hcDC,hBitmap,0,dwY,lpBuf,&b,DIB_RGB_COLORS);
{
::ReleaseDC(NULL,hDesktopDC);
LocalFree(bmpInf);
return -2;
}
cout<<"buf size 2 = "<<sizeof(buf)<<endl;
::ReleaseDC(NULL,hDesktopDC);
CString sMsg;
sMsg.Format(_T("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)
{
cout<<"1111111111111111111111111111111111111"<<endl;
Sleep(1000);
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)); //测试显示
cout<<(int)rgbQ.rgbRed<<","<<(int)rgbQ.rgbGreen<<","<<(int)rgbQ.rgbBlue<<"\t";
}
cout<<endl;
}
}
else if(bmpInf->bmiHeader.biBitCount == 16)
{
cout<<"22222222222222222222222222222222222222"<<endl;
Sleep(1000);
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)); //测试显示
cout<<(int)r<<","<<(int)g<<","<<(int)b<<"\t";
}
cout<<endl;
}
}
else if(bmpInf->bmiHeader.biBitCount == 24)
{
cout<<"333333333333333333333333333333333333333333333"<<endl;
Sleep(1000);
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)); //测试显示
cout<<(int)r<<","<<(int)g<<","<<(int)b<<"\t";
}
cout<<endl;
}
}
else if(bmpInf->bmiHeader.biBitCount == 32)
{
cout<<"44444444444444444444444444444444444444444444444444"<<endl;
Sleep(1000);
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)); //测试显示
cout<<(int)r<<","<<(int)g<<","<<(int)b<<"\t";
}
cout<<endl;
}
}
DeleteObject(hBmp);
LocalFree(bmpInf);
return 0;
