1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/tools/screenshot/win32-screenshot.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,110 @@ 1.4 +/* 1.5 + * Copyright (c) 2009, The Mozilla Foundation 1.6 + * All rights reserved. 1.7 + * 1.8 + * Redistribution and use in source and binary forms, with or without 1.9 + * modification, are permitted provided that the following conditions are met: 1.10 + * * Redistributions of source code must retain the above copyright 1.11 + * notice, this list of conditions and the following disclaimer. 1.12 + * * Redistributions in binary form must reproduce the above copyright 1.13 + * notice, this list of conditions and the following disclaimer in the 1.14 + * documentation and/or other materials provided with the distribution. 1.15 + * * Neither the name of the Mozilla Foundation nor the 1.16 + * names of its contributors may be used to endorse or promote products 1.17 + * derived from this software without specific prior written permission. 1.18 + * 1.19 + * THIS SOFTWARE IS PROVIDED BY The Mozilla Foundation ''AS IS'' AND ANY 1.20 + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 1.21 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 1.22 + * DISCLAIMED. IN NO EVENT SHALL The Mozilla Foundation BE LIABLE FOR ANY 1.23 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 1.24 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 1.25 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 1.26 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.27 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.28 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.29 + * 1.30 + * Contributors: 1.31 + * Ted Mielczarek <ted.mielczarek@gmail.com> 1.32 + */ 1.33 +/* 1.34 + * screenshot.cpp : Save a screenshot of the Windows desktop in .png format. 1.35 + * If a filename is specified as the first argument on the commandline, 1.36 + * then the image will be saved to that filename. Otherwise, the image will 1.37 + * be saved as "screenshot.png" in the current working directory. 1.38 + */ 1.39 + 1.40 +#undef WIN32_LEAN_AND_MEAN 1.41 +#include <windows.h> 1.42 +#include <gdiplus.h> 1.43 + 1.44 +using namespace Gdiplus; 1.45 + 1.46 +// From http://msdn.microsoft.com/en-us/library/ms533843%28VS.85%29.aspx 1.47 +static int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) 1.48 +{ 1.49 + UINT num = 0; // number of image encoders 1.50 + UINT size = 0; // size of the image encoder array in bytes 1.51 + 1.52 + ImageCodecInfo* pImageCodecInfo = nullptr; 1.53 + 1.54 + GetImageEncodersSize(&num, &size); 1.55 + if(size == 0) 1.56 + return -1; // Failure 1.57 + 1.58 + pImageCodecInfo = (ImageCodecInfo*)(malloc(size)); 1.59 + if(pImageCodecInfo == nullptr) 1.60 + return -1; // Failure 1.61 + 1.62 + GetImageEncoders(num, size, pImageCodecInfo); 1.63 + 1.64 + for(UINT j = 0; j < num; ++j) 1.65 + { 1.66 + if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 ) 1.67 + { 1.68 + *pClsid = pImageCodecInfo[j].Clsid; 1.69 + free(pImageCodecInfo); 1.70 + return j; // Success 1.71 + } 1.72 + } 1.73 + 1.74 + free(pImageCodecInfo); 1.75 + return -1; // Failure 1.76 +} 1.77 + 1.78 +#ifdef __MINGW32__ 1.79 +extern "C" 1.80 +#endif 1.81 +int wmain(int argc, wchar_t** argv) 1.82 +{ 1.83 + GdiplusStartupInput gdiplusStartupInput; 1.84 + ULONG_PTR gdiplusToken; 1.85 + GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr); 1.86 + 1.87 + HWND desktop = GetDesktopWindow(); 1.88 + HDC desktopdc = GetDC(desktop); 1.89 + HDC mydc = CreateCompatibleDC(desktopdc); 1.90 + int width = GetSystemMetrics(SM_CXSCREEN); 1.91 + int height = GetSystemMetrics(SM_CYSCREEN); 1.92 + HBITMAP mybmp = CreateCompatibleBitmap(desktopdc, width, height); 1.93 + HBITMAP oldbmp = (HBITMAP)SelectObject(mydc, mybmp); 1.94 + BitBlt(mydc,0,0,width,height,desktopdc,0,0, SRCCOPY|CAPTUREBLT); 1.95 + SelectObject(mydc, oldbmp); 1.96 + 1.97 + const wchar_t* filename = (argc > 1) ? argv[1] : L"screenshot.png"; 1.98 + Bitmap* b = Bitmap::FromHBITMAP(mybmp, nullptr); 1.99 + CLSID encoderClsid; 1.100 + Status stat = GenericError; 1.101 + if (b && GetEncoderClsid(L"image/png", &encoderClsid) != -1) { 1.102 + stat = b->Save(filename, &encoderClsid, nullptr); 1.103 + } 1.104 + if (b) 1.105 + delete b; 1.106 + 1.107 + // cleanup 1.108 + GdiplusShutdown(gdiplusToken); 1.109 + ReleaseDC(desktop, desktopdc); 1.110 + DeleteObject(mybmp); 1.111 + DeleteDC(mydc); 1.112 + return stat == Ok ? 0 : 1; 1.113 +}