Horizon – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Drawing;
3 using System.Drawing.Imaging;
4 using System.IO;
5 using System.Runtime.InteropServices;
6 using System.Windows.Forms;
7  
8 namespace Horizon.Utilities
9 {
10 public enum CaptureMode
11 {
12 Screen,
13  
14 Window
15 }
16  
17 /// <summary>
18 /// Capture screen or active window.
19 /// </summary>
20 /// <remarks>KvanTTT & Wade Hatler @ https://stackoverflow.com/questions/1163761/capture-screenshot-of-active-window</remarks>
21 public static class ScreenCapture
22 {
23 #region Static Fields and Constants
24  
25 /// <summary> Position of the cursor relative to the start of the capture </summary>
26 public static Point CursorPosition;
27  
28 #endregion
29  
30 #region Nested Types
31  
32 [StructLayout(LayoutKind.Sequential)]
33 private struct Rect
34 {
35 public readonly int Left;
36  
37 public readonly int Top;
38  
39 public readonly int Right;
40  
41 public readonly int Bottom;
42 }
43  
44 #endregion
45  
46 #region Public Methods
47  
48 [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
49 public static extern IntPtr GetDesktopWindow();
50  
51 /// <summary>
52 /// Capture Active Window, Desktop, Window or Control by hWnd or .NET Contro/Form and save it to a specified
53 /// file.
54 /// </summary>
55 /// <param name="filename">
56 /// Filename.
57 /// <para>* If extension is omitted, it's calculated from the type of file</para>
58 /// <para>* If path is omitted, defaults to %TEMP%</para>
59 /// <para>* Use %NOW% to put a timestamp in the filename</para>
60 /// </param>
61 /// <param name="mode">Optional. The default value is CaptureMode.Window.</param>
62 /// <param name="format">Optional file save mode. Default is PNG</param>
63 public static void CaptureAndSave(string filename, CaptureMode mode = CaptureMode.Window,
64 ImageFormat format = null)
65 {
66 ImageSave(filename, format, Capture(mode));
67 }
68  
69 /// <summary> Capture a specific window (or control) and save it to a specified file. </summary>
70 /// <param name="filename">
71 /// Filename.
72 /// <para>* If extension is omitted, it's calculated from the type of file</para>
73 /// <para>* If path is omitted, defaults to %TEMP%</para>
74 /// <para>* Use %NOW% to put a timestamp in the filename</para>
75 /// </param>
76 /// <param name="handle">hWnd (handle) of the window to capture</param>
77 /// <param name="format">Optional file save mode. Default is PNG</param>
78 public static void CaptureAndSave(string filename, IntPtr handle, ImageFormat format = null)
79 {
80 ImageSave(filename, format, Capture(handle));
81 }
82  
83 /// <summary> Capture a specific window (or control) and save it to a specified file. </summary>
84 /// <param name="filename">
85 /// Filename.
86 /// <para>* If extension is omitted, it's calculated from the type of file</para>
87 /// <para>* If path is omitted, defaults to %TEMP%</para>
88 /// <para>* Use %NOW% to put a timestamp in the filename</para>
89 /// </param>
90 /// <param name="c">Object to capture</param>
91 /// <param name="format">Optional file save mode. Default is PNG</param>
92 public static void CaptureAndSave(string filename, Control c, ImageFormat format = null)
93 {
94 ImageSave(filename, format, Capture(c));
95 }
96  
97 /// <summary> Capture the active window (default) or the desktop and return it as a bitmap </summary>
98 /// <param name="mode">Optional. The default value is CaptureMode.Window.</param>
99 public static Bitmap Capture(CaptureMode mode = CaptureMode.Window)
100 {
101 return Capture(mode == CaptureMode.Screen ? GetDesktopWindow() : GetForegroundWindow());
102 }
103  
104 /// <summary> Capture a .NET Control, Form, UserControl, etc. </summary>
105 /// <param name="c">Object to capture</param>
106 /// <returns> Bitmap of control's area </returns>
107 public static Bitmap Capture(Control c)
108 {
109 return Capture(c.Handle);
110 }
111  
112  
113 /// <summary> Capture a specific window and return it as a bitmap </summary>
114 /// <param name="handle">hWnd (handle) of the window to capture</param>
115 public static Bitmap Capture(IntPtr handle)
116 {
117 Rectangle bounds;
118 var rect = new Rect();
119 GetWindowRect(handle, ref rect);
120 bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
121 CursorPosition = new Point(Cursor.Position.X - rect.Left, Cursor.Position.Y - rect.Top);
122  
123 var result = new Bitmap(bounds.Width, bounds.Height);
124 using (var graphics = Graphics.FromImage(result))
125 {
126 graphics.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
127 }
128  
129 return result;
130 }
131  
132 #endregion
133  
134 #region Private Methods
135  
136 [DllImport("user32.dll")]
137 private static extern IntPtr GetForegroundWindow();
138  
139 [DllImport("user32.dll")]
140 private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
141  
142  
143 /// <summary> Save an image to a specific file </summary>
144 /// <param name="filename">
145 /// Filename.
146 /// <para>* If extension is omitted, it's calculated from the type of file</para>
147 /// <para>* If path is omitted, defaults to %TEMP%</para>
148 /// <para>* Use %NOW% to put a timestamp in the filename</para>
149 /// </param>
150 /// <param name="format">Optional file save mode. Default is PNG</param>
151 /// <param name="image">
152 /// Image to save. Usually a BitMap, but can be any
153 /// Image.
154 /// </param>
155 private static void ImageSave(string filename, ImageFormat format, Image image)
156 {
157 if (format == null)
158 {
159 format = ImageFormat.Png;
160 }
161  
162 if (!filename.Contains("."))
163 {
164 filename = filename.Trim() + "." + format.ToString().ToLower();
165 }
166  
167 if (!filename.Contains(@"\"))
168 {
169 filename = Path.Combine(Environment.GetEnvironmentVariable("TEMP") ?? @"C:\Temp", filename);
170 }
171  
172 filename = filename.Replace("%NOW%", DateTime.Now.ToString("yyyy-MM-dd@hh.mm.ss"));
173 image.Save(filename, format);
174 }
175  
176 #endregion
177 }
178 }