#include<windows.h>
void ScreenSnap(HBITMAP hBitmap,char *bmpPath,HDC dc);
int main()
{
HWND DeskWnd=::GetDesktopWindow();//获取桌面窗口句柄
RECT DeskRC;
::GetClientRect(DeskWnd,&DeskRC);//获取窗口大小
HDC DeskDC=GetDC(DeskWnd);//获取窗口DC
HBITMAP DeskBmp=::CreateCompatibleBitmap(DeskDC,DeskRC.right,DeskRC.bottom);//兼容位图
HDC memDC=::CreateCompatibleDC(DeskDC);//兼容DC
SelectObject(memDC,DeskBmp);//把兼容位图选入兼容DC中
BitBlt(memDC,0,0,DeskRC.right,DeskRC.bottom,DeskDC,0,0,SRCCOPY);//拷贝DC
ScreenSnap(DeskBmp,"d:\\Screen.bmp",DeskDC);
return 0;
}
void ScreenSnap(HBITMAP hBitmap,char *bmpPath,HDC dc)
{
BITMAP bmInfo;
DWORD bmDataSize;
char *bmData;//位图数据
GetObject(hBitmap,sizeof(BITMAP),&bmInfo);//根据位图句柄,获取位图信息
bmDataSize=bmInfo.bmWidthBytes*bmInfo.bmHeight;//计算位图数据大小
bmData=new char[bmDataSize];//分配数据
BITMAPFILEHEADER bfh;//位图文件头
bfh.bfType=0x4d42;
bfh.bfSize=bmDataSize+54;
bfh.bfReserved1=0;
bfh.bfReserved2=0;
bfh.bfOffBits=54;
BITMAPINFOHEADER bih;//位图信息头
bih.biSize=40; //该结构大小
bih.biWidth=bmInfo.bmWidth;
bih.biHeight=bmInfo.bmHeight;
bih.biPlanes=1; /*必须为1*/
bih.biBitCount=24; /*24位*/
bih.biCompression=BI_RGB;
bih.biSizeImage=bmDataSize;
bih.biXPelsPerMeter=0;
bih.biYPelsPerMeter=0;
bih.biClrUsed=0;
bih.biClrImportant=0;
::GetDIBits(dc,hBitmap,0,bmInfo.bmHeight,bmData,(BITMAPINFO *)&bih,DIB_RGB_COLORS);//获取位图数据部分
HANDLE hFile=CreateFile(bmpPath,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,0);//创建文件
DWORD dwSize;
WriteFile(hFile,(void *)&bfh,sizeof(BITMAPFILEHEADER),&dwSize,0);//写入位图文件头
WriteFile(hFile,(void *)&bih,sizeof(BITMAPINFOHEADER),&dwSize,0);//写入位图信息头
WriteFile(hFile,(void *)bmData,bmDataSize,&dwSize,0);//写入位图数据
::CloseHandle(hFile);//关闭文件句柄
}
截屏保存为图片
最新推荐文章于 2021-06-08 10:57:06 发布