window_snapshot_win.cc revision ddb351dbec246cf1fab5ec20d2d5520909041de1
1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/ui/window_snapshot/window_snapshot.h"
6
7#include "base/win/scoped_gdi_object.h"
8#include "base/win/scoped_hdc.h"
9#include "ui/gfx/codec/png_codec.h"
10#include "ui/gfx/gdi_util.h"
11#include "ui/gfx/rect.h"
12#include "ui/gfx/size.h"
13
14namespace browser {
15
16gfx::Rect GrabWindowSnapshot(gfx::NativeWindow window_handle,
17                             std::vector<unsigned char>* png_representation) {
18  // Create a memory DC that's compatible with the window.
19  HDC window_hdc = GetWindowDC(window_handle);
20  base::win::ScopedHDC mem_hdc(CreateCompatibleDC(window_hdc));
21
22  // Create a DIB that's the same size as the window.
23  RECT content_rect = {0, 0, 0, 0};
24  ::GetWindowRect(window_handle, &content_rect);
25  content_rect.right++;  // Match what PrintWindow wants.
26  int width = content_rect.right - content_rect.left;
27  int height = content_rect.bottom - content_rect.top;
28  BITMAPINFOHEADER hdr;
29  gfx::CreateBitmapHeader(width, height, &hdr);
30  unsigned char *bit_ptr = NULL;
31  base::win::ScopedBitmap bitmap(
32      CreateDIBSection(mem_hdc,
33                       reinterpret_cast<BITMAPINFO*>(&hdr),
34                       DIB_RGB_COLORS,
35                       reinterpret_cast<void **>(&bit_ptr),
36                       NULL, 0));
37
38  SelectObject(mem_hdc, bitmap);
39  // Clear the bitmap to white (so that rounded corners on windows
40  // show up on a white background, and strangely-shaped windows
41  // look reasonable). Not capturing an alpha mask saves a
42  // bit of space.
43  PatBlt(mem_hdc, 0, 0, width, height, WHITENESS);
44  // Grab a copy of the window
45  // First, see if PrintWindow is defined (it's not in Windows 2000).
46  typedef BOOL (WINAPI *PrintWindowPointer)(HWND, HDC, UINT);
47  PrintWindowPointer print_window =
48      reinterpret_cast<PrintWindowPointer>(
49          GetProcAddress(GetModuleHandle(L"User32.dll"), "PrintWindow"));
50
51  // If PrintWindow is defined, use it.  It will work on partially
52  // obscured windows, and works better for out of process sub-windows.
53  // Otherwise grab the bits we can get with BitBlt; it's better
54  // than nothing and will work fine in the average case (window is
55  // completely on screen).
56  if (print_window)
57    (*print_window)(window_handle, mem_hdc, 0);
58  else
59    BitBlt(mem_hdc, 0, 0, width, height, window_hdc, 0, 0, SRCCOPY);
60
61  // We now have a copy of the window contents in a DIB, so
62  // encode it into a useful format for posting to the bug report
63  // server.
64  gfx::PNGCodec::Encode(bit_ptr, gfx::PNGCodec::FORMAT_BGRA,
65                        gfx::Size(width, height), width * 4, true,
66                        std::vector<gfx::PNGCodec::Comment>(),
67                        png_representation);
68
69  ReleaseDC(window_handle, window_hdc);
70
71  return gfx::Rect(width, height);
72}
73
74}  // namespace browser
75