windows截屏

 #ifndef _CAPTURESCREEN_H
#define _CAPTURESCREEN_H #include <windows.h>
class CaptureScreen
{
public:
CaptureScreen()
{};
~CaptureScreen(void)
{}; void ScreenInit();
void ScreenFree();
bool ScreenSetWindow(LPRECT lpRect);
bool ScreenCopy(BYTE *dstBuf); int rHeight; //要获得的图像尺寸
int rWidth;
private:
int nScreenWidth;
int nScreenHeight;
PRGBTRIPLE scan0;
BITMAPINFO pbi;
int stride;
int top_x;
int top_y;
int bot_x;
int bot_y;
}; #endif
 #include "CaptureScreen.h"

 void CaptureScreen::ScreenInit()
{
nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
nScreenHeight = GetSystemMetrics(SM_CYSCREEN); pbi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbi.bmiHeader.biWidth = nScreenWidth;
pbi.bmiHeader.biHeight = nScreenHeight;
pbi.bmiHeader.biPlanes = ;
pbi.bmiHeader.biBitCount = ;
pbi.bmiHeader.biCompression = BI_RGB;
stride = ((nScreenWidth * + ) & 0xffffffe0) / ; // 24位图像扫描线宽度
scan0 = (PRGBTRIPLE)malloc(stride * nScreenHeight); // 图像数据缓冲区,应释放
} void CaptureScreen::ScreenFree()
{
free(scan0);
} bool CaptureScreen::ScreenSetWindow(LPRECT lpRect)
{
top_x = lpRect->left;
top_y = lpRect->top;
bot_x = lpRect->right;
bot_y = lpRect->bottom;
if(top_x >= bot_x)
return false;
if(top_y >= bot_y)
return false;
if(top_x< || bot_x>nScreenWidth)
return false;
if(top_y< || bot_y>nScreenHeight)
return false; rHeight = bot_y-top_y;
rWidth = bot_x-top_x;
return true;
} bool CaptureScreen::ScreenCopy(BYTE *dstBuf)
{
HWND hDesktopWnd = GetDesktopWindow();
HDC hDesktopDC = GetDC(hDesktopWnd);
HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC,nScreenWidth, nScreenHeight);
SelectObject(hCaptureDC,hCaptureBitmap);
BitBlt(hCaptureDC,,,nScreenWidth,nScreenHeight,hDesktopDC,,,SRCCOPY|NOMIRRORBITMAP);
DeleteDC(hCaptureDC); GetDIBits(hDesktopDC, hCaptureBitmap, , nScreenHeight, scan0, &pbi, DIB_RGB_COLORS);
ReleaseDC(hDesktopWnd,hDesktopDC);
DeleteObject(hCaptureBitmap); BYTE *srcBuf = (BYTE*) scan0; int j;
srcBuf += (nScreenHeight--top_y)*stride+top_x*;// scan0倒序
for(j=;j<rHeight;j++)
{
memcpy(dstBuf,srcBuf,rWidth*);
dstBuf += rWidth*;
srcBuf -= stride;
}
return true;
}
 #include "CaptureScreen.h"
#include <stdio.h>
#include <Mmsystem.h>
#pragma comment( lib,"winmm.lib" )
void SaveBmp2(BYTE *bgr32,int w,int h,int mode)
{ /////////////////////////////////////////////
DWORD size=w*h*;
LPSTR lpData=(LPSTR)GlobalAlloc(GPTR,size);
/////////////////////////////////////////////
BITMAPINFOHEADER bih;
bih.biSize=sizeof(BITMAPINFOHEADER);
bih.biWidth=w;
bih.biHeight=h;
bih.biPlanes=;
bih.biBitCount=;
bih.biCompression=;
bih.biSizeImage=w*h*;//size;
bih.biXPelsPerMeter=;
bih.biYPelsPerMeter=;
bih.biClrUsed=;
bih.biClrImportant=; BITMAPFILEHEADER bfh;
bfh.bfType=0x4D42; // "BM" // 设置位图文件头
bfh.bfReserved1=bfh.bfReserved2=;
bfh.bfSize=+size;
bfh.bfOffBits=; if(mode == )
{
for(int i=;i<h;i++)
{
memcpy(lpData+(h--i)*w*,bgr32+i*w*,w*);
}
}
else
memcpy(lpData,bgr32,w*h*); static int filecount=;
char filename[];
sprintf(filename,"C:\\%d.bmp",filecount);
FILE *fp=fopen(filename,"wb"); if(fp)
{ fwrite(&bfh,,sizeof(BITMAPFILEHEADER),fp);
fwrite(&bih,,sizeof(BITMAPINFOHEADER),fp);
fwrite(lpData,,size,fp);
fclose(fp);
filecount++;
}
GlobalFree(lpData); } int main()
{ CaptureScreen myCapture;
myCapture.ScreenInit();
RECT dstRect={,,,};
myCapture.ScreenSetWindow(&dstRect);
BYTE *dstBuff = (BYTE*)malloc(myCapture.rWidth*myCapture.rHeight*);
int lastTime = timeGetTime();
myCapture.ScreenCopy(dstBuff);
printf("time:%dms",timeGetTime()-lastTime);
SaveBmp2((BYTE*)dstBuff,myCapture.rWidth,myCapture.rHeight,);
myCapture.ScreenFree(); return ;
}
上一篇:PAT甲级 1001 A+B Format


下一篇:[C++]PAT乙级1001.害死人不偿命的(3n+1)猜想(15/15)