DSDibHelper.cxx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #if !defined(_WIN32_WCE)
  2. //------------------------------------------------------------------------------
  3. // File: DibHelper.cpp
  4. //
  5. // Desc: DirectShow sample code - In-memory push mode source filter
  6. // Helper routines for manipulating bitmaps.
  7. //
  8. // Copyright (c) Microsoft Corporation. All rights reserved.
  9. //------------------------------------------------------------------------------
  10. #include <windows.h>
  11. #include "dsdibhelper.h"
  12. HBITMAP CopyScreenToBitmap(LPRECT lpRect, BYTE *pData, BITMAPINFO *pHeader)
  13. {
  14. HDC hScrDC, hMemDC; // screen DC and memory DC
  15. HBITMAP hBitmap, hOldBitmap; // handles to deice-dependent bitmaps
  16. int nX, nY, nX2, nY2; // coordinates of rectangle to grab
  17. int nWidth, nHeight; // DIB width and height
  18. int xScrn, yScrn; // screen resolution
  19. // check for an empty rectangle
  20. if (IsRectEmpty(lpRect)) {
  21. return NULL;
  22. }
  23. // create a DC for the screen and create
  24. // a memory DC compatible to screen DC
  25. hScrDC = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
  26. hMemDC = CreateCompatibleDC(hScrDC);
  27. // get points of rectangle to grab
  28. nX = lpRect->left;
  29. nY = lpRect->top;
  30. nX2 = lpRect->right;
  31. nY2 = lpRect->bottom;
  32. // get screen resolution
  33. xScrn = GetDeviceCaps(hScrDC, HORZRES);
  34. yScrn = GetDeviceCaps(hScrDC, VERTRES);
  35. //make sure bitmap rectangle is visible
  36. if (nX < 0) {
  37. nX = 0;
  38. }
  39. if (nY < 0) {
  40. nY = 0;
  41. }
  42. if (nX2 > xScrn) {
  43. nX2 = xScrn;
  44. }
  45. if (nY2 > yScrn) {
  46. nY2 = yScrn;
  47. }
  48. nWidth = nX2 - nX;
  49. nHeight = nY2 - nY;
  50. // create a bitmap compatible with the screen DC
  51. hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
  52. // select new bitmap into memory DC
  53. hOldBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);
  54. // bitblt screen DC to memory DC
  55. BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);
  56. // select old bitmap back into memory DC and get handle to
  57. // bitmap of the screen
  58. hBitmap = (HBITMAP) SelectObject(hMemDC, hOldBitmap);
  59. // Copy the bitmap data into the provided BYTE buffer
  60. GetDIBits(hScrDC, hBitmap, 0, nHeight, pData, pHeader, DIB_RGB_COLORS);
  61. // clean up
  62. DeleteDC(hScrDC);
  63. DeleteDC(hMemDC);
  64. // return handle to the bitmap
  65. return hBitmap;
  66. }
  67. #endif /* _WIN32_WCE */