|
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 * 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 |
|
37 #undef WIN32_LEAN_AND_MEAN |
|
38 #include <windows.h> |
|
39 #include <gdiplus.h> |
|
40 |
|
41 using namespace Gdiplus; |
|
42 |
|
43 // From http://msdn.microsoft.com/en-us/library/ms533843%28VS.85%29.aspx |
|
44 static int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) |
|
45 { |
|
46 UINT num = 0; // number of image encoders |
|
47 UINT size = 0; // size of the image encoder array in bytes |
|
48 |
|
49 ImageCodecInfo* pImageCodecInfo = nullptr; |
|
50 |
|
51 GetImageEncodersSize(&num, &size); |
|
52 if(size == 0) |
|
53 return -1; // Failure |
|
54 |
|
55 pImageCodecInfo = (ImageCodecInfo*)(malloc(size)); |
|
56 if(pImageCodecInfo == nullptr) |
|
57 return -1; // Failure |
|
58 |
|
59 GetImageEncoders(num, size, pImageCodecInfo); |
|
60 |
|
61 for(UINT j = 0; j < num; ++j) |
|
62 { |
|
63 if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 ) |
|
64 { |
|
65 *pClsid = pImageCodecInfo[j].Clsid; |
|
66 free(pImageCodecInfo); |
|
67 return j; // Success |
|
68 } |
|
69 } |
|
70 |
|
71 free(pImageCodecInfo); |
|
72 return -1; // Failure |
|
73 } |
|
74 |
|
75 #ifdef __MINGW32__ |
|
76 extern "C" |
|
77 #endif |
|
78 int wmain(int argc, wchar_t** argv) |
|
79 { |
|
80 GdiplusStartupInput gdiplusStartupInput; |
|
81 ULONG_PTR gdiplusToken; |
|
82 GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr); |
|
83 |
|
84 HWND desktop = GetDesktopWindow(); |
|
85 HDC desktopdc = GetDC(desktop); |
|
86 HDC mydc = CreateCompatibleDC(desktopdc); |
|
87 int width = GetSystemMetrics(SM_CXSCREEN); |
|
88 int height = GetSystemMetrics(SM_CYSCREEN); |
|
89 HBITMAP mybmp = CreateCompatibleBitmap(desktopdc, width, height); |
|
90 HBITMAP oldbmp = (HBITMAP)SelectObject(mydc, mybmp); |
|
91 BitBlt(mydc,0,0,width,height,desktopdc,0,0, SRCCOPY|CAPTUREBLT); |
|
92 SelectObject(mydc, oldbmp); |
|
93 |
|
94 const wchar_t* filename = (argc > 1) ? argv[1] : L"screenshot.png"; |
|
95 Bitmap* b = Bitmap::FromHBITMAP(mybmp, nullptr); |
|
96 CLSID encoderClsid; |
|
97 Status stat = GenericError; |
|
98 if (b && GetEncoderClsid(L"image/png", &encoderClsid) != -1) { |
|
99 stat = b->Save(filename, &encoderClsid, nullptr); |
|
100 } |
|
101 if (b) |
|
102 delete b; |
|
103 |
|
104 // cleanup |
|
105 GdiplusShutdown(gdiplusToken); |
|
106 ReleaseDC(desktop, desktopdc); |
|
107 DeleteObject(mybmp); |
|
108 DeleteDC(mydc); |
|
109 return stat == Ok ? 0 : 1; |
|
110 } |