asmrobot
2019-11-21 589ed88a5924a7494e21b95b6bbff5e46ff49ddd
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
 
 
namespace RichCreator.Utility.Captures
{
    public class BitBltCapture:ICapture
    {
        /// <summary>
        /// 截屏
        /// </summary>
        /// <returns></returns>
        public Bitmap CaptureScreen()
        {
            return CaptureWindow(Utils.GetDesktopWindow(),-1,-1,-1,-1);
        }
 
        /// <summary>
        /// 截取指定区域
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public Bitmap CaptureScreen(Int32 x,Int32 y,Int32 width,Int32 height)
        {
            return CaptureWindow(Utils.GetDesktopWindow(),x,y,width,height);
        }
 
        /// <summary>
        /// Creates an Image object containing a screen shot of a specific window
        /// </summary>
        /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
        /// <returns></returns>
        private static Bitmap CaptureWindow(IntPtr handle,Int32 x,Int32 y,Int32 width,Int32 height)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = Utils.GetWindowDC(handle);
            // get the size
            Utils.Rect rect = new Utils.Rect();
            Utils.GetWindowRect(handle, ref rect);
            int screenWidth = 0;
            int screenHeight = 0;
            Utils.GetWindowSize(out screenWidth, out screenHeight);
            if (x < 0 || y < 0 || width <= 0 || height <= 0 || (x + width) > screenWidth || (y + height) > screenHeight)
            {
                x = 0;
                y = 0;
                width = screenWidth;
                height = screenHeight;
            }
 
 
            // create a device context we can copy to
            IntPtr hdcDest = Utils.CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = Utils.CreateCompatibleBitmap(hdcSrc, width, height);
            // select the bitmap object
            IntPtr hOld = Utils.SelectObject(hdcDest, hBitmap);
            // bitblt over
            Utils.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, x, y, Utils.SRCCOPY);
            // restore selection
            Utils.SelectObject(hdcDest, hOld);
            // clean up
            Utils.DeleteDC(hdcDest);
            Utils.ReleaseDC(handle, hdcSrc);
            // get a .NET image object for it
 
            Bitmap img = Bitmap.FromHbitmap(hBitmap);
            // free up the Bitmap object
            Utils.DeleteObject(hBitmap);
 
 
            return img;
        }
        
 
 
        /// <summary>
        /// Captures a screen shot of the entire desktop, and saves it to a file
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="format"></param>
        public void CaptureScreenToFile(string fileName)
        {
            CaptureScreenToFile(fileName, ImageFormat.Jpeg);
        }
 
        /// <summary>
        /// Captures a screen shot of the entire desktop, and saves it to a file
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="format"></param>
        public void CaptureScreenToFile(string fileName, ImageFormat format)
        {
            Bitmap bitmap = CaptureScreen();
            bitmap.Save(fileName, format);
        }
 
 
 
    }
}