与上一篇相对应 截取桌面屏幕返回位图的各信息
void CatchScrBmp(BITMAP &m_bmpBit1,BYTE* &m_pBmpData1,BITMAPINFO &BitmapInfo)
{
HDC hScrDC, hMemDC; //屏幕,内存设备描述表句柄
HBITMAP hBmp; //位图句柄
int nWidth, nHeight; //屏幕的宽和高
hScrDC = ::CreateDC(_T("DISPLAY"), NULL, NULL, NULL); //创建屏幕设备描述表句柄
hMemDC = ::CreateCompatibleDC(hScrDC); //创建与屏幕设备相兼容的内存设备描述表
//分别得到屏幕的宽和高
nWidth = ::GetDeviceCaps(
hScrDC,
HORZRES // HORZRES Width, in pixels, of the screen.
// VERTRES Height, in raster lines, of the screen.
);
nHeight = ::GetDeviceCaps(hScrDC, VERTRES);
//创建与屏幕设备相兼容的位图
hBmp = ::CreateCompatibleBitmap(
hScrDC, // handle to DC
nWidth, // width of bitmap, in pixels
nHeight // height of bitmap, in pixels
);
::SelectObject(hMemDC, hBmp); //将位图选入内存设备描述表
//复制屏幕设备描述表到内存设备描述表
::BitBlt(
hMemDC, // handle to destination DC
0, // x-coord of destination upper-left corner
0, // y-coord of destination upper-left corner
nWidth, // width of destination rectangle
nHeight, // height of destination rectangle
hScrDC, // handle to source DC
0, // x-coordinate of source upper-left corner
0, // y-coordinate of source upper-left corner
SRCCOPY // raster operation code
);
CBitmap Bmp;
Bmp.Attach(hBmp);//根据位图句柄,得到位图
BITMAP Bitmap; //位图结构
Bmp.GetBitmap(&Bitmap); //得到位图信息头和位图颜色信息
//位图信息结构,包含位图信息头和位图颜色信息
//位图信息头结构
BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFO); //本结构所占用的字节数
BitmapInfo.bmiHeader.biWidth = Bitmap.bmWidth; //位图宽度,以像素为单位
BitmapInfo.bmiHeader.biHeight = Bitmap.bmHeight; //位图高度,以像素为单位
BitmapInfo.bmiHeader.biPlanes = 1; //目标设备的级别,必须为1
BitmapInfo.bmiHeader.biBitCount = 24; //每个像素所需的位数,必须为1,4,8,24,32
BitmapInfo.bmiHeader.biCompression = 0; //位图压缩类型,必须为0,1,2
BitmapInfo.bmiHeader.biSizeImage = Bitmap.bmWidthBytes * Bitmap.bmHeight; //位图大小,以字节为单位
BitmapInfo.bmiHeader.biXPelsPerMeter = 0; //位图水平分辨率,每米像素数
BitmapInfo.bmiHeader.biYPelsPerMeter = 0; //位图垂直分辩率,每米象素数
BitmapInfo.bmiHeader.biClrUsed = 0; //位图实际使用的颜色表中的颜色数
BitmapInfo.bmiHeader.biClrImportant = 0; //位图显示过程中重要的颜色数
//位图数据信息
m_pBmpData1 = new BYTE[Bitmap.bmWidthBytes * Bitmap.bmHeight]; //用于保存位图数据的缓冲区
int n = ::GetDIBits(
hMemDC, // handle to DC
hBmp, // handle to bitmap
0, // first scan line to set
Bitmap.bmHeight, // number of scan lines to copy
m_pBmpData1, // array for bitmap bits
&BitmapInfo, // bitmap data buffer
DIB_RGB_COLORS // RGB or palette index
);
m_bmpBit1 = Bitmap;
//创建了句柄,一定要释放,否则会浪费内存
::DeleteDC(hScrDC);
::DeleteDC(hMemDC);
}