windows截屏
时间:2014-06-25 15:20:15
收藏:0
阅读:345
1 #ifndef _CAPTURESCREEN_H 2 #define _CAPTURESCREEN_H 3 4 #include <windows.h> 5 class CaptureScreen 6 { 7 public: 8 CaptureScreen() 9 {}; 10 ~CaptureScreen(void) 11 {}; 12 13 void ScreenInit(); 14 void ScreenFree(); 15 bool ScreenSetWindow(LPRECT lpRect); 16 bool ScreenCopy(BYTE *dstBuf); 17 18 int rHeight; //要获得的图像尺寸 19 int rWidth; 20 private: 21 int nScreenWidth; 22 int nScreenHeight; 23 PRGBTRIPLE scan0; 24 BITMAPINFO pbi; 25 int stride; 26 int top_x; 27 int top_y; 28 int bot_x; 29 int bot_y; 30 }; 31 32 #endif
1 #include "CaptureScreen.h" 2 3 void CaptureScreen::ScreenInit() 4 { 5 nScreenWidth = GetSystemMetrics(SM_CXSCREEN); 6 nScreenHeight = GetSystemMetrics(SM_CYSCREEN); 7 8 pbi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 9 pbi.bmiHeader.biWidth = nScreenWidth; 10 pbi.bmiHeader.biHeight = nScreenHeight; 11 pbi.bmiHeader.biPlanes = 1; 12 pbi.bmiHeader.biBitCount = 24; 13 pbi.bmiHeader.biCompression = BI_RGB; 14 stride = ((nScreenWidth * 24 + 31) & 0xffffffe0) / 8; // 24位图像扫描线宽度 15 scan0 = (PRGBTRIPLE)malloc(stride * nScreenHeight); // 图像数据缓冲区,应释放 16 } 17 18 void CaptureScreen::ScreenFree() 19 { 20 free(scan0); 21 } 22 23 bool CaptureScreen::ScreenSetWindow(LPRECT lpRect) 24 { 25 top_x = lpRect->left; 26 top_y = lpRect->top; 27 bot_x = lpRect->right; 28 bot_y = lpRect->bottom; 29 if(top_x >= bot_x) 30 return false; 31 if(top_y >= bot_y) 32 return false; 33 if(top_x<0 || bot_x>nScreenWidth) 34 return false; 35 if(top_y<0 || bot_y>nScreenHeight) 36 return false; 37 38 rHeight = bot_y-top_y; 39 rWidth = bot_x-top_x; 40 return true; 41 } 42 43 bool CaptureScreen::ScreenCopy(BYTE *dstBuf) 44 { 45 HWND hDesktopWnd = GetDesktopWindow(); 46 HDC hDesktopDC = GetDC(hDesktopWnd); 47 HDC hCaptureDC = CreateCompatibleDC(hDesktopDC); 48 HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC,nScreenWidth, nScreenHeight); 49 SelectObject(hCaptureDC,hCaptureBitmap); 50 BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,hDesktopDC,0,0,SRCCOPY|NOMIRRORBITMAP); 51 DeleteDC(hCaptureDC); 52 53 GetDIBits(hDesktopDC, hCaptureBitmap, 0, nScreenHeight, scan0, &pbi, DIB_RGB_COLORS); 54 ReleaseDC(hDesktopWnd,hDesktopDC); 55 DeleteObject(hCaptureBitmap); 56 57 BYTE *srcBuf = (BYTE*) scan0; 58 59 int j; 60 srcBuf += (nScreenHeight-1-top_y)*stride+top_x*3;// scan0倒序 61 for(j=0;j<rHeight;j++) 62 { 63 memcpy(dstBuf,srcBuf,rWidth*3); 64 dstBuf += rWidth*3; 65 srcBuf -= stride; 66 } 67 return true; 68 }
1 #include "CaptureScreen.h" 2 #include <stdio.h> 3 #include <Mmsystem.h> 4 #pragma comment( lib,"winmm.lib" ) 5 void SaveBmp2(BYTE *bgr32,int w,int h,int mode) 6 { 7 8 ///////////////////////////////////////////// 9 DWORD size=w*h*3; 10 LPSTR lpData=(LPSTR)GlobalAlloc(GPTR,size); 11 ///////////////////////////////////////////// 12 BITMAPINFOHEADER bih; 13 bih.biSize=sizeof(BITMAPINFOHEADER); 14 bih.biWidth=w; 15 bih.biHeight=h; 16 bih.biPlanes=1; 17 bih.biBitCount=24; 18 bih.biCompression=0; 19 bih.biSizeImage=w*h*3;//size; 20 bih.biXPelsPerMeter=0; 21 bih.biYPelsPerMeter=0; 22 bih.biClrUsed=0; 23 bih.biClrImportant=0; 24 25 BITMAPFILEHEADER bfh; 26 bfh.bfType=0x4D42; // "BM" // 设置位图文件头 27 bfh.bfReserved1=bfh.bfReserved2=0; 28 bfh.bfSize=54+size; 29 bfh.bfOffBits=54; 30 31 if(mode == 0) 32 { 33 for(int i=0;i<h;i++) 34 { 35 memcpy(lpData+(h-1-i)*w*3,bgr32+i*w*3,w*3); 36 } 37 } 38 else 39 memcpy(lpData,bgr32,w*h*3); 40 41 static int filecount=0; 42 char filename[255]; 43 sprintf(filename,"C:\\%d.bmp",filecount); 44 FILE *fp=fopen(filename,"wb"); 45 46 if(fp) 47 { 48 49 fwrite(&bfh,1,sizeof(BITMAPFILEHEADER),fp); 50 fwrite(&bih,1,sizeof(BITMAPINFOHEADER),fp); 51 fwrite(lpData,1,size,fp); 52 fclose(fp); 53 filecount++; 54 } 55 GlobalFree(lpData); 56 57 } 58 59 int main() 60 { 61 62 CaptureScreen myCapture; 63 myCapture.ScreenInit(); 64 RECT dstRect={0,0,1920,1080}; 65 myCapture.ScreenSetWindow(&dstRect); 66 BYTE *dstBuff = (BYTE*)malloc(myCapture.rWidth*myCapture.rHeight*3); 67 int lastTime = timeGetTime(); 68 myCapture.ScreenCopy(dstBuff); 69 printf("time:%dms",timeGetTime()-lastTime); 70 SaveBmp2((BYTE*)dstBuff,myCapture.rWidth,myCapture.rHeight,0); 71 myCapture.ScreenFree(); 72 73 74 return 0; 75 }
评论(0)