testing/tools/screenshot/win32-screenshot.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial