win32-screenshot.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2009, The Mozilla Foundation
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the Mozilla Foundation nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY The Mozilla Foundation ''AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL The Mozilla Foundation BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * Contributors:
  28. * Ted Mielczarek <ted.mielczarek@gmail.com>
  29. */
  30. /*
  31. * win32-screenshot.cpp: Save a screenshot of the Windows desktop in .png format.
  32. * If a filename is specified as the first argument on the commandline,
  33. * then the image will be saved to that filename. Otherwise, the image will
  34. * be saved as "screenshot.png" in the current working directory.
  35. */
  36. // VS2015: Platform SDK 8.1's GdiplusTypes.h uses the min macro
  37. #undef NOMINMAX
  38. #undef WIN32_LEAN_AND_MEAN
  39. #include <windows.h>
  40. #include <gdiplus.h>
  41. // Link w/ subsystem windows so we don't get a console when executing
  42. // this binary.
  43. #pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:wmainCRTStartup")
  44. using namespace Gdiplus;
  45. // From http://msdn.microsoft.com/en-us/library/ms533843%28VS.85%29.aspx
  46. static int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
  47. {
  48. UINT num = 0; // number of image encoders
  49. UINT size = 0; // size of the image encoder array in bytes
  50. ImageCodecInfo* pImageCodecInfo = nullptr;
  51. GetImageEncodersSize(&num, &size);
  52. if(size == 0)
  53. return -1; // Failure
  54. pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
  55. if(pImageCodecInfo == nullptr)
  56. return -1; // Failure
  57. GetImageEncoders(num, size, pImageCodecInfo);
  58. for(UINT j = 0; j < num; ++j)
  59. {
  60. if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
  61. {
  62. *pClsid = pImageCodecInfo[j].Clsid;
  63. free(pImageCodecInfo);
  64. return j; // Success
  65. }
  66. }
  67. free(pImageCodecInfo);
  68. return -1; // Failure
  69. }
  70. #ifdef __MINGW32__
  71. extern "C"
  72. #endif
  73. int wmain(int argc, wchar_t** argv)
  74. {
  75. GdiplusStartupInput gdiplusStartupInput;
  76. ULONG_PTR gdiplusToken;
  77. GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr);
  78. HWND desktop = GetDesktopWindow();
  79. HDC desktopdc = GetDC(desktop);
  80. HDC mydc = CreateCompatibleDC(desktopdc);
  81. int width = GetSystemMetrics(SM_CXSCREEN);
  82. int height = GetSystemMetrics(SM_CYSCREEN);
  83. HBITMAP mybmp = CreateCompatibleBitmap(desktopdc, width, height);
  84. HBITMAP oldbmp = (HBITMAP)SelectObject(mydc, mybmp);
  85. BitBlt(mydc,0,0,width,height,desktopdc,0,0, SRCCOPY|CAPTUREBLT);
  86. SelectObject(mydc, oldbmp);
  87. const wchar_t* filename = (argc > 1) ? argv[1] : L"screenshot.png";
  88. Bitmap* b = Bitmap::FromHBITMAP(mybmp, nullptr);
  89. CLSID encoderClsid;
  90. Status stat = GenericError;
  91. if (b && GetEncoderClsid(L"image/png", &encoderClsid) != -1) {
  92. stat = b->Save(filename, &encoderClsid, nullptr);
  93. }
  94. if (b)
  95. delete b;
  96. // cleanup
  97. GdiplusShutdown(gdiplusToken);
  98. ReleaseDC(desktop, desktopdc);
  99. DeleteObject(mybmp);
  100. DeleteDC(mydc);
  101. return stat == Ok ? 0 : 1;
  102. }