using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace Tango.ScreenCapture { public class BitBltScreenCapture : IScreenCaptureMethod { #region Win32 API Screen shot calls // Win32 API calls necessary to support screen capture [DllImport("gdi32", EntryPoint = "BitBlt", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] private static extern int BitBlt(int hDestDC, int x, int y, int nWidth, int nHeight, int hSrcDC, int xSrc, int ySrc, int dwRop); [DllImport("user32", EntryPoint = "GetDC", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] private static extern int GetDC(int hwnd); [DllImport("user32", EntryPoint = "ReleaseDC", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] private static extern int ReleaseDC(int hwnd, int hdc); #endregion public Bitmap GetDesktopBitmap(CaptureRegion region) { const int SRCCOPY = 13369376; Bitmap bitmap = new Bitmap(region.Width, region.Height); using (Graphics g = Graphics.FromImage(bitmap)) { // Get a device context to the windows desktop and our destination bitmaps int hdcSrc = GetDC(0); IntPtr hdcDest = g.GetHdc(); // Copy what is on the desktop to the bitmap BitBlt(hdcDest.ToInt32(), 0, 0, region.Width, region.Height, hdcSrc, 0, 0, SRCCOPY); // Release device contexts g.ReleaseHdc(hdcDest); ReleaseDC(0, hdcSrc); } return bitmap; } public void Dispose() { //Do nothing. } } }