Guest User

Untitled

a guest
Jun 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. HBITMAP CaptureAnImage(HWND hWnd, UINT nWidth, UINT nHeight)
  2. {
  3. HDC hdcScreen;
  4. HDC hdcWindow;
  5. HDC hdcMemDC = NULL;
  6. HBITMAP hbmScreen = NULL;
  7. BITMAP bmpScreen;
  8.  
  9. // Retrieve the handle to a display device context for the client
  10. // area of the window.
  11. hdcScreen = GetDC(NULL);
  12. hdcWindow = GetDC(hWnd);
  13.  
  14. // Create a compatible DC which is used in a BitBlt from the window DC
  15. hdcMemDC = CreateCompatibleDC(hdcWindow);
  16.  
  17. if (!hdcMemDC)
  18. {
  19. MessageBox(hWnd, L"CreateCompatibleDC has failed", L"Failed", MB_OK);
  20. goto done;
  21. }
  22.  
  23. //This is the best stretch mode
  24. SetStretchBltMode(hdcWindow, HALFTONE);
  25.  
  26. //The source DC is the entire screen and the destination DC is the current window (HWND)
  27. if (!StretchBlt(hdcWindow,
  28. 0, 0,
  29. nWidth, nHeight,
  30. hdcScreen,
  31. 0, 0,
  32. GetSystemMetrics(SM_CXSCREEN),
  33. GetSystemMetrics(SM_CYSCREEN),
  34. SRCCOPY))
  35. {
  36. MessageBox(hWnd, L"StretchBlt has failed", L"Failed", MB_OK);
  37. goto done;
  38. }
  39.  
  40. // Create a compatible bitmap from the Window DC
  41. hbmScreen = CreateCompatibleBitmap(hdcWindow, nWidth, nHeight);
  42.  
  43. if (!hbmScreen)
  44. {
  45. MessageBox(hWnd, L"CreateCompatibleBitmap Failed", L"Failed", MB_OK);
  46. goto done;
  47. }
  48.  
  49. // Select the compatible bitmap into the compatible memory DC.
  50. SelectObject(hdcMemDC, hbmScreen);
  51.  
  52. // Bit block transfer into our compatible memory DC.
  53. if (!BitBlt(hdcMemDC,
  54. 0, 0,
  55. nWidth, nHeight,
  56. hdcWindow,
  57. 0, 0,
  58. SRCCOPY))
  59. {
  60. MessageBox(hWnd, L"BitBlt has failed", L"Failed", MB_OK);
  61. goto done;
  62. }
  63.  
  64. // Get the BITMAP from the HBITMAP
  65. GetObject(hbmScreen, sizeof(BITMAP), &bmpScreen);
  66.  
  67. BITMAPFILEHEADER bmfHeader;
  68. BITMAPINFOHEADER bi;
  69.  
  70. bi.biSize = sizeof(BITMAPINFOHEADER);
  71. bi.biWidth = bmpScreen.bmWidth;
  72. bi.biHeight = bmpScreen.bmHeight;
  73. bi.biPlanes = 1;
  74. bi.biBitCount = 32;
  75. bi.biCompression = BI_RGB;
  76. bi.biSizeImage = 0;
  77. bi.biXPelsPerMeter = 0;
  78. bi.biYPelsPerMeter = 0;
  79. bi.biClrUsed = 0;
  80. bi.biClrImportant = 0;
  81.  
  82. DWORD dwBmpSize = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpScreen.bmHeight;
  83.  
  84. // Starting with 32-bit Windows, GlobalAlloc and LocalAlloc are implemented as wrapper functions that
  85. // call HeapAlloc using a handle to the process's default heap. Therefore, GlobalAlloc and LocalAlloc
  86. // have greater overhead than HeapAlloc.
  87. HANDLE hDIB = GlobalAlloc(GHND, dwBmpSize);
  88. char *lpbitmap = (char *)GlobalLock(hDIB);
  89.  
  90. // Gets the "bits" from the bitmap and copies them into a buffer
  91. // which is pointed to by lpbitmap.
  92. GetDIBits(hdcWindow, hbmScreen, 0,
  93. (UINT)bmpScreen.bmHeight,
  94. lpbitmap,
  95. (BITMAPINFO *)&bi, DIB_RGB_COLORS);
  96.  
  97. // A file is created, this is where we will save the screen capture.
  98. HANDLE hFile = CreateFile(L"captureqwsx.bmp",
  99. GENERIC_WRITE,
  100. 0,
  101. NULL,
  102. CREATE_ALWAYS,
  103. FILE_ATTRIBUTE_NORMAL, NULL);
  104.  
  105. // Add the size of the headers to the size of the bitmap to get the total file size
  106. DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  107.  
  108. //Offset to where the actual bitmap bits start.
  109. bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
  110.  
  111. //Size of the file
  112. bmfHeader.bfSize = dwSizeofDIB;
  113.  
  114. //bfType must always be BM for Bitmaps
  115. bmfHeader.bfType = 0x4D42; //BM
  116.  
  117. DWORD dwBytesWritten = 0;
  118. WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
  119. WriteFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
  120. WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);
  121.  
  122. //Unlock and Free the DIB from the heap
  123. GlobalUnlock(hDIB);
  124. GlobalFree(hDIB);
  125.  
  126. //Close the handle for the file that was created
  127. CloseHandle(hFile);
  128.  
  129. //Clean up
  130. done:
  131. //DeleteObject(hbmScreen);
  132. DeleteObject(hdcMemDC);
  133. ReleaseDC(NULL, hdcScreen);
  134. ReleaseDC(hWnd, hdcWindow);
  135.  
  136. return hbmScreen;
  137. }
  138.  
  139.  
  140. HBITMAP GetWndSnapshot(HWND hWnd)
  141. {
  142. if (!hWnd) return NULL;
  143.  
  144. RECT rect;
  145. ::GetWindowRect(hWnd, &rect);
  146.  
  147. int nWidth = rect.right - rect.left;
  148. int nHeight = rect.bottom - rect.top;
  149.  
  150. HDC hDC = ::GetWindowDC(hWnd);
  151. HDC memDC = ::CreateCompatibleDC(hDC);
  152. HBITMAP hBitmap = ::CreateCompatibleBitmap(hDC, nWidth, nHeight);
  153. HBITMAP oldBitmap = (HBITMAP)::SelectObject(memDC, hBitmap);
  154. ::BitBlt(memDC, 0, 0, nWidth, nHeight, hDC, 0, 0, SRCCOPY);
  155. ::SelectObject(memDC, oldBitmap);
  156. ::DeleteDC(memDC);
  157. ::ReleaseDC(hWnd, hDC);
  158.  
  159. return hBitmap;
  160. }
  161.  
  162.  
  163. HBITMAP Create32BppDIBSection(int nWidth, int nHeight)
  164. {
  165. BITMAPINFO bmi;
  166. memset(&bmi, 0, sizeof(bmi));
  167. bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  168. bmi.bmiHeader.biWidth = nWidth;
  169. bmi.bmiHeader.biHeight = -nHeight;
  170. bmi.bmiHeader.biPlanes = 1;
  171. bmi.bmiHeader.biBitCount = 32;
  172. bmi.bmiHeader.biCompression = BI_RGB;
  173.  
  174. LPVOID pvBits = NULL;
  175. return CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, &pvBits, NULL, 0);
  176. }
  177.  
  178. HBITMAP ConvertBitmap24To32(HBITMAP hbmp24)
  179. {
  180. if (!hbmp24) return NULL;
  181.  
  182. BITMAP bm24;
  183. if (!GetObject(hbmp24, sizeof(BITMAP), &bm24)) return NULL;
  184. if (bm24.bmBitsPixel != 24) return NULL;
  185.  
  186. HBITMAP hbmp32 = Create32BppDIBSection(bm24.bmWidth, bm24.bmHeight);
  187. if (!hbmp32) return NULL;
  188.  
  189. BITMAP bm32;
  190. if (!GetObject(hbmp32, sizeof(BITMAP), &bm32)) return NULL;
  191.  
  192. for (int i = 0; i < bm32.bmHeight; ++i)
  193. {
  194. RGBQUAD *pRowDst = (RGBQUAD *)((LPBYTE)bm32.bmBits + (bm32.bmHeight - 1 - i) * bm32.bmWidthBytes);
  195. RGBTRIPLE *pRowSrc = (RGBTRIPLE *)((LPBYTE)bm24.bmBits + i * bm24.bmWidthBytes);
  196. for (int j = 0; j < bm32.bmWidth; ++j)
  197. {
  198. pRowDst[j].rgbBlue = pRowSrc[j].rgbtBlue;
  199. pRowDst[j].rgbGreen = pRowSrc[j].rgbtGreen;
  200. pRowDst[j].rgbRed = pRowSrc[j].rgbtRed;
  201. }
  202. }
  203.  
  204. return hbmp32;
  205. }
Add Comment
Please, Sign In to add comment