using System;
2using System.Runtime.InteropServices;
3using System.Drawing;
4using System.Drawing.Imaging;
5
6namespace ScreenShotDemo
7{
8 /**//// <summary>
9 /// 提供捕获全屏或者一个不规则窗口函数,并保存。
10 /// </summary>
11 public class ScreenCapture
12 {
13 /**//// <summary>
14 /// Creates an Image object containing a screen shot of the entire desktop?
15 /// </summary>
16 /// <returns></returns>
17 public Image CaptureScreen()
18 {
19 return CaptureWindow( User32.GetDesktopWindow() );
20 }
21
22 /**//// <summary>
23 /// Creates an Image object containing a screen shot of a specific window?
24 /// </summary>
25 /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
26 /// <returns></returns>
27 public Image CaptureWindow(IntPtr handle)
28 {
29 // get te hDC of the target window
30 IntPtr hdcSrc = User32.GetWindowDC(handle);
31 // get the size
32 User32.RECT windowRect = new User32.RECT();
33 User32.GetWindowRect(handle,ref windowRect);
34 int width = windowRect.right - windowRect.left;
35 int height = windowRect.bottom - windowRect.top;
36 // create a device context we can copy to
37 IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
38 // create a bitmap we can copy it to,
39 // using GetDeviceCaps to get the width/height
40 IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,width,height);
41 // select the bitmap object
42 IntPtr hOld = GDI32.SelectObject(hdcDest,hBitmap);
43 // bitblt over
44 GDI32.BitBlt(hdcDest,0,0,width,height,hdcSrc,0,0,GDI32.SRCCOPY);
45 // restore selection
46 GDI32.SelectObject(hdcDest,hOld);
47 // clean up
48 GDI32.DeleteDC(hdcDest);
49 User32.ReleaseDC(handle,hdcSrc);
50
51 // get a .NET p_w_picpath object for it
52 Image img = Image.FromHbitmap(hBitmap);
53 // free up the Bitmap object
54 GDI32.DeleteObject(hBitmap);
55
56 return img;
57 }
58
59 /**//// <summary>
60 /// Captures a screen shot of a specific window, and saves it to a file?
61 /// </summary>
62 /// <param name="handle"></param>
63 /// <param name="filename"></param>
64 /// <param name="format"></param>
65 public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format)
66 {
67 Image img = CaptureWindow(handle);
68 img.Save(filename,format);
69 }
70
71 /**//// <summary>
72 /// Captures a screen shot of the entire desktop, and saves it to a file?
73 /// </summary>
74 /// <param name="filename"></param>
75 /// <param name="format"></param>
76 public void CaptureScreenToFile(string filename, ImageFormat format)
77 {
78 Image img = CaptureScreen();
79 img.Save(filename,format);
80 }
81
82 /**//// <summary>
83 /// Helper class containing Gdi32 API functions
84 /// </summary>
85 private class GDI32
86 {
87
88 public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
89
90 [DllImport("gdi32.dll")]
91 public static extern bool BitBlt(IntPtr hObject,int nXDest,int nYDest,
92 int nWidth,int nHeight,IntPtr hObjectSource,
93 int nXSrc,int nYSrc,int dwRop);
94 [DllImport("gdi32.dll")]
95 public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC,int nWidth,
96 int nHeight);
97 [DllImport("gdi32.dll")]
98 public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
99 [DllImport("gdi32.dll")]
100 public static extern bool DeleteDC(IntPtr hDC);
101 [DllImport("gdi32.dll")]
102 public static extern bool DeleteObject(IntPtr hObject);
103 [DllImport("gdi32.dll")]
104 public static extern IntPtr SelectObject(IntPtr hDC,IntPtr hObject);
105 }
106
107 /**//// <summary>
108 /// Helper class containing User32 API functions
109 /// </summary>
110 private class User32
111 {
112 [StructLayout(LayoutKind.Sequential)]
113 public struct RECT
114 {
115 public int left;
116 public int top;
117 public int right;
118 public int bottom;
119 }
120
121 [DllImport("user32.dll")]
122 public static extern IntPtr GetDesktopWindow();
123 [DllImport("user32.dll")]
124 public static extern IntPtr GetWindowDC(IntPtr hWnd);
125 [DllImport("user32.dll")]
126 public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDC);
127 [DllImport("user32.dll")]
128 public static extern IntPtr GetWindowRect(IntPtr hWnd,ref RECT rect);
129
130 }
131
132
133 }
134}
135
136http://www.xinbcw.com
C#截屏程序
精选 转载上一篇:java 复制目录代码
下一篇:C#解一元二次方程
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C#截屏操作
/// <summary> /// 截全屏并保存成图片 /// </summ
desktop 工作区 全屏 -
C#模拟QQ截屏功能
可以利用Graphics类的CopyFromScreen方法来实现屏幕截取,舍弃了比较麻烦的API函数,只要几句代码就能实现了,而且这个这个方法能实现只抓取部分区
c# desktop 背景图 ip协议 小程序 -
C# 编写简单易用的 Windows 截屏增强工具
前言半年前我开源了 DreamScene2 一个小而快并且功能强大的 Windows 动态桌面软件使
c# windows 开发语言 键盘钩子 句柄