aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.RemoteDesktop/CaptureMethods/BitBltScreenCapture.cs
blob: d8f1e214c2532711313f5b3942696674cd5f0d9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
{
    /// <summary>
    /// Represents a BitBlt screen capture method.
    /// </summary>
    /// <seealso cref="Tango.RemoteDesktop.ICaptureMethod" />
    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

        /// <summary>
        /// Gets the desktop bitmap.
        /// </summary>
        /// <param name="region">The capture region.</param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        public void Dispose()
        {
            //Do nothing.
        }
    }
}