Download, last updated 27/08/2017 (28.55 KB)

  • md5: d5feac405f75f196cbed10213fbb31bc
  • sha1: 4c93ad2973fc7edf65a36515a295d8d2846bad86
  • sha256: 5d05b91c2ce2699dd39bdba6660dfe834aefac64837f02acfa0568b46c146de5
// Capturing screenshots using C# and p/invoke
// Copyright © 2017 Cyotek Ltd. All Rights Reserved.

// This work is licensed under the Creative Commons Attribution 4.0 International License.
// To view a copy of this license, visit

using System;
using System.Runtime.InteropServices;

// ReSharper disable FieldCanBeMadeReadOnly.Global
// ReSharper disable InconsistentNaming

namespace Cyotek.Demo.SimpleScreenshotCapture
  internal static class NativeMethods
    #region Externals

    public static extern bool BitBlt(IntPtr hdcDest, int nxDest, int nyDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, RasterOperations dwRop);

    public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);

    public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

    public static extern IntPtr DeleteDC(IntPtr hdc);

    public static extern IntPtr DeleteObject(IntPtr hObject);

    public static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out RECT pvAttribute, int cbAttribute);

    public static extern IntPtr GetDesktopWindow();

    public static extern IntPtr GetForegroundWindow();

    public static extern IntPtr GetWindowDC(IntPtr hWnd);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);

    public static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc);

    public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hObject);


    #region RasterOperations enum

    public enum RasterOperations
      SRCCOPY = 0x00CC0020,

      SRCPAINT = 0x00EE0086,

      SRCAND = 0x008800C6,

      SRCINVERT = 0x00660046,

      SRCERASE = 0x00440328,

      NOTSRCCOPY = 0x00330008,

      NOTSRCERASE = 0x001100A6,

      MERGECOPY = 0x00C000CA,

      MERGEPAINT = 0x00BB0226,

      PATCOPY = 0x00F00021,

      PATPAINT = 0x00FB0A09,

      PATINVERT = 0x005A0049,

      DSTINVERT = 0x00550009,

      BLACKNESS = 0x00000042,

      WHITENESS = 0x00FF0062,

      CAPTUREBLT = 0x40000000 //only if WinVer >= 5.0.0 (see wingdi.h)


    #region Constants

    public const int DWMWA_EXTENDED_FRAME_BOUNDS = 9;


    #region Nested type: RECT

    public struct RECT
      public int teft;

      public int top;

      public int bight;

      public int bottom;