利用win api 实现截屏 保存为BMP并转为字节流

c++ 利用winAPI 实现截屏幕;

代码:

#include
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;
bih.biBitCount=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);//关闭文件句柄
}


实现某一区域截屏 并转为字节流:

int sumSize;
char* getScreen(long left,long top,long right,long bottom){

	HWND DeskWnd=::GetDesktopWindow();//获取桌面窗口句柄
	RECT DeskRC;
	DeskRC.left = left;
	DeskRC.top = top;
	DeskRC.right = right;
	DeskRC.bottom = bottom;

//	::GetClientRect(DeskWnd,&DeskRC);//获取窗口大小
	HDC DeskDC=GetDC(DeskWnd);//获取窗口DC
	HBITMAP DeskBmp=::CreateCompatibleBitmap(DeskDC,DeskRC.right-DeskRC.left,DeskRC.bottom-DeskRC.top);//兼容位图
	HDC memDC=::CreateCompatibleDC(DeskDC);//兼容DC
	SelectObject(memDC,DeskBmp);//把兼容位图选入兼容DC中
	BitBlt(memDC,0,0,right-DeskRC.left,DeskRC.bottom-DeskRC.top,DeskDC,DeskRC.left,DeskRC.top,SRCCOPY);//拷贝DC

	BITMAP bmInfo;
	DWORD bmDataSize;
	char *bmData;//位图数据
	GetObject(DeskBmp,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;
	bih.biBitCount=24;
	bih.biCompression=BI_RGB;
	bih.biSizeImage=bmDataSize;
	bih.biXPelsPerMeter=0;
	bih.biYPelsPerMeter=0;
	bih.biClrUsed=0;
	bih.biClrImportant=0;

	::GetDIBits(DeskDC,DeskBmp,0,bmInfo.bmHeight,bmData,(BITMAPINFO *)&bih,DIB_RGB_COLORS);//获取位图数据部分

    sumSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bmDataSize;
    char * stream = new char[sumSize];
	memcpy(stream,&bfh,sizeof(BITMAPFILEHEADER));
	memcpy(stream+sizeof(BITMAPFILEHEADER),&bih,sizeof(BITMAPINFOHEADER));
	memcpy(stream+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER),bmData,bmDataSize);

	HANDLE hFile=CreateFile("d:\\Screen.bmp",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,
	FILE_ATTRIBUTE_NORMAL,0);//创建文件
	DWORD dwSize;
	WriteFile(hFile,(void *)stream,sumSize,&dwSize,0);//写入位图文件头
	::CloseHandle(hFile);//关闭文件句柄



	return stream;

}


你可能感兴趣的:(C++)