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.RemoteDesktop.CaptureMethods
{
///
/// Represents a BitBlt screen capture method.
///
///
public class BitBltScreenCapture : ICaptureMethod
{
#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
///
/// Gets the desktop bitmap.
///
/// The capture region.
///
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;
}
///
/// Releases unmanaged and - optionally - managed resources.
///
public void Dispose()
{
//Do nothing.
}
}
}