windows截屏
2020-12-13 03:23
标签:des style class blog code color windows截屏,搜素材,soscw.com windows截屏 标签:des style class blog code color 原文地址:http://www.cnblogs.com/mlj318/p/3806531.html 1 #ifndef _CAPTURESCREEN_H
2 #define _CAPTURESCREEN_H
3
4 #include
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_x0 || bot_x>nScreenWidth)
34 return false;
35 if(top_y0 || 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
1 #include "CaptureScreen.h"
2 #include