-
Notifications
You must be signed in to change notification settings - Fork 1
/
ComposedScreenshot.cs
336 lines (270 loc) · 15.4 KB
/
ComposedScreenshot.cs
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using FMUtils.WinApi;
namespace FMUtils.Screenshot
{
public class ComposedScreenshot : Screenshot
{
public bool withBorderRounding
{
get
{
return _withborderrounding;
}
set
{
if (_withborderrounding != value)
{
_composedScreenshotImage = null;
_composedScreenshotThumbnail = null;
}
_withborderrounding = value;
}
}
bool _withborderrounding = false;
public bool withBorderShadow
{
get
{
return _withbordershadow;
}
set
{
if (_withbordershadow != value)
{
_composedScreenshotImage = null;
_composedScreenshotThumbnail = null;
}
_withbordershadow = value;
}
}
bool _withbordershadow = false;
public bool withCursor
{
get
{
return _withcursor;
}
set
{
if (_withcursor != value)
{
_composedScreenshotImage = null;
_composedScreenshotThumbnail = null;
}
_withcursor = value;
}
}
bool _withcursor = false;
public Point CursorLocation { get; private set; }
private Bitmap CursorImage;
public List<Screenshot> CompositionStack = new List<Screenshot>();
public Rectangle CompositionRect
{
get
{
if (CompositionStack.Count == 0)
return Rectangle.Empty;
var StackAggregate = CompositionStack.Select((ss, i) =>
{
if (!withBorderShadow)
return ss.CaptureRect.IsEmpty ? ss.TargetRect : ss.CaptureRect;
else
return i == 0 ? CompositionHelper.GetTargetRectWithShadow(ss.CaptureRect) : CompositionHelper.GetTargetRectWithMenuShadow(ss.TargetRect);
}).Aggregate((result, r) => Rectangle.Union(result, r));
return withCursor ? Rectangle.Union(StackAggregate, new Rectangle(CursorLocation, CursorImage.Size)) : StackAggregate;
}
}
public Point CompositionOrigin
{
get
{
Point result = Point.Empty;
foreach (var ss in CompositionStack)
{
if (CompositionStack.IndexOf(ss) == 0)
{
result = ss.TargetRect.Location;
}
else
{
result.X = ss.TargetRect.Location.X < result.X ? ss.TargetRect.Location.X : result.X;
result.Y = ss.TargetRect.Location.Y < result.Y ? ss.TargetRect.Location.Y : result.Y;
}
}
return result;
}
}
public Bitmap ComposedScreenshotImage
{
get
{
if (_composedScreenshotImage == null)
_composedScreenshotImage = GetComposedScreenshotImage();
return _composedScreenshotImage;
}
}
Bitmap _composedScreenshotImage = null;
public Bitmap ComposedScreenshotThumbnail
{
get
{
if (_composedScreenshotThumbnail == null)
_composedScreenshotThumbnail = GetComposedScreenshotThumbnail();
return _composedScreenshotThumbnail;
}
}
Bitmap _composedScreenshotThumbnail = null;
private Rectangle r;
public ComposedScreenshot(ScreenshotMethod method = ScreenshotMethod.DWM, bool withSolidGlass = true)
: this(IntPtr.Zero, method, withSolidGlass) { }
public ComposedScreenshot(IntPtr targetWindow, ScreenshotMethod method = ScreenshotMethod.DWM, bool withSolidGlass = true)
: base(targetWindow, method, withSolidGlass)
{
Trace.WriteLine("Creating composed screenshot...", string.Format("ComposedScreenshot.ctor [{0}]", System.Threading.Thread.CurrentThread.Name));
CursorLocation = Cursor.Position;
CursorImage = Core.GetCursor();
//#32768 is the Windows Menu Class
// || wc.StartsWith("WindowsForms10.Window.20808");
var ContextMenus = Windowing.GetChildWindows(Windowing.GetDesktopWindow()).Where(h => Windowing.GetWindowClass(h) == "#32768").ToList();
if (ContextMenus.Any(h => CaptureRect.IntersectsWith(Helper.GetWindowRectangle(h))))
{
Trace.WriteLine(string.Format("Found Win32 {0} menus...", ContextMenus.Count), string.Format("ComposedScreenshot.ctor [{0}]", System.Threading.Thread.CurrentThread.Name));
CompositionStack.AddRange(ContextMenus.Select(h => Screenshot.FromBitmapRect(Core.ScreenshotWindow(h), Helper.GetWindowRectangle((h)))));
}
else
{
//If there are no Win32 popup context menus found, but the mouse is over something who's root is the target rect, walk that tree for composition items
//Check if the thing the mouse is over is different (eg, an overhanging menu)
IntPtr MouseTargetHandle = Windowing.WindowFromPoint(Cursor.Position);
//We don't want to do this when we're trying to get popup context menus since they are children of the desktop window,
//not the target window. However that situation is handled separately above.
//What we're doing here is looking for overlapping windows, like GTK context menus
if (Windowing.GetAncestor(MouseTargetHandle, Windowing.GA_ROOTOWNER) == this.TargetHandle) // && !this.TargetRect.Contains(Cursor.Position))
{
Trace.WriteLine(string.Format("Mouse over something else, handle {0}...", MouseTargetHandle), string.Format("ComposedScreenshot.ctor [{0}]", System.Threading.Thread.CurrentThread.Name));
while (MouseTargetHandle != TargetHandle && MouseTargetHandle != IntPtr.Zero)
{
//var MouseTargetWindowRect = Helper.GetWindowRectangle(MouseTargetHandle);
//var MouseTargetScreenshot = Core.ScreenshotArea(MouseTargetHandle, MouseTargetWindowRect);
//GTK menus seem to have virtually identical parents of themselves, so I initially tried to circumvent this
//with a check to prevent any two same rects from being added
//though, it's obsolete with the more robust removal below
//if (!CompositionStack.Any(ss => ss.TargetRect == MouseTargetWindowRect))
//var layer = Screenshot.FromBitmapRect(MouseTargetScreenshot, MouseTargetWindowRect);
var layer = new Screenshot(MouseTargetHandle, ScreenshotMethod.GDI, false);
CompositionStack.Add(layer);
Trace.WriteLine("Added " + layer.TargetHandle.ToString() + "; Class: " + layer.WindowClass, "ComposedScreenshot.ctor");
MouseTargetHandle = Windowing.GetParent(MouseTargetHandle);
}
Trace.WriteLine(string.Format("Added {0} items...", CompositionStack.Count), string.Format("ComposedScreenshot.ctor [{0}]", System.Threading.Thread.CurrentThread.Name));
//Remove any rect which is completely inside of another. This is mainly an effort to avoid extra layers,
//such as those generated by GTK menus, where some elements appear to be duplicated.
var toRemove = CompositionStack.Where(cs => CompositionStack.Any(a => cs != a && a.TargetRect.Contains(cs.TargetRect))).ToList();
Trace.WriteLine(string.Format("Removing {0} items...", toRemove.Count), string.Format("ComposedScreenshot.ctor [{0}]", System.Threading.Thread.CurrentThread.Name));
//In a case where there's one menu and two identical rects, don't remove both of them, or we'll have nothing to compose
if (toRemove.Count == CompositionStack.Count && toRemove.Count > 0)
toRemove.RemoveAt(toRemove.Count - 1);
CompositionStack.RemoveAll(r => toRemove.Contains(r));
//If they're all fully inside the window bounds, remove them- probably just regular window elements
if (CompositionStack.All(cs => this.TargetRect.Contains(cs.TargetRect)))
{
Trace.WriteLine(string.Format("Clearing {0} items...", toRemove.Count), string.Format("ComposedScreenshot.ctor [{0}]", System.Threading.Thread.CurrentThread.Name));
//CompositionStack.RemoveAll(cs => !cs.WindowClass.StartsWith("WindowsForms10.Window.20808"));
CompositionStack.Clear();
}
}
}
//Since we compose where first is bottom and last is top, we'll need to reverse what we have so far since we enumerate the other direction
CompositionStack.Reverse();
//Base window is always the bottom layer of the composition stack
CompositionStack.Insert(0, this);
Trace.WriteLine("Done.", string.Format("ComposedScreenshot.ctor [{0}]", System.Threading.Thread.CurrentThread.Name));
}
public ComposedScreenshot(Rectangle r)
: base(r)
{
Trace.WriteLine("Creating region screenshot...", string.Format("ComposedScreenshot.ctor [{0}]", System.Threading.Thread.CurrentThread.Name));
CursorLocation = Cursor.Position;
CursorImage = Core.GetCursor();
//CompositionStack.Add(new Screenshot(r));
CompositionStack.Add(Screenshot.FromBitmapRect(Core.ScreenshotArea(r), r));
Trace.WriteLine("Done.", string.Format("ComposedScreenshot.ctor [{0}]", System.Threading.Thread.CurrentThread.Name));
}
private Bitmap GetComposedScreenshotImage()
{
Trace.WriteLine(string.Format("Composing {0} images...", CompositionStack.Count), string.Format("ComposedScreenshot.GetComposedScreenshotImage [{0}]", System.Threading.Thread.CurrentThread.Name));
Bitmap Composite = new Bitmap(CompositionRect.Width, CompositionRect.Height);
using (var g = Graphics.FromImage(Composite))
{
//todo:
//1- capture handle's classes
//2- in here, apply rounding and shadowing as applicable per flags & known class (eg, for windows, the window shadow. for menu's the menu shadow. else, none)
var ShadowOffset = withBorderShadow ? CompositionHelper.GetBaseLayerDeltaOffsetWithShadow() : Point.Empty;
for (int i = 0; i < CompositionStack.Count; i++)
{
var WorkingBitmap = CompositionStack[i].BaseScreenshotImage.Clone() as Bitmap;
if (i == 0)
{
if (withBorderRounding && !isMaximized)
WorkingBitmap = CompositionHelper.GetImageWithRoundedBorders(WorkingBitmap);
if (withBorderShadow)
WorkingBitmap = CompositionHelper.GetImageWithShadow(WorkingBitmap);
g.DrawImageUnscaled(WorkingBitmap, new Point(CompositionStack[i].TargetRect.Location.X - CompositionOrigin.X, CompositionStack[i].TargetRect.Location.Y - CompositionOrigin.Y));
}
else
{
if (withBorderShadow)
{
var r = CompositionStack[i].TargetRect;
r.Intersect(CompositionStack[0].TargetRect);
//We only need to clip the occlusion if the screenshot method grabbed any overlaying menus as part of the base screenshot
//This doesn't happen when we create it via DWM since then, we solely grab the window
//But it ALSO doesn't happen when the visual theme is Basic/Classic. Why? No idea! The shadow is painted on by some mechanism that doesn't capture
WorkingBitmap = CompositionHelper.GetImageWithMenuShadow(WorkingBitmap, new Rectangle(r.X - CompositionStack[i].TargetRect.X, r.Y - CompositionStack[i].TargetRect.Y, r.Width, r.Height), this.Method != ScreenshotMethod.DWM && FMUtils.WinApi.Helper.VisualStyle == Helper.VisualStyles.Aero);
}
g.DrawImageUnscaled(WorkingBitmap, new Point(CompositionStack[i].TargetRect.Location.X - CompositionOrigin.X + ShadowOffset.X, CompositionStack[i].TargetRect.Location.Y - CompositionOrigin.Y + ShadowOffset.Y));
}
if (Directory.Exists(Core.DebugPath))
{
g.Flush();
g.Save();
WorkingBitmap.Save(Path.Combine(Core.DebugPath, "item-" + i.ToString() + ".png"), System.Drawing.Imaging.ImageFormat.Png);
Composite.Save(Path.Combine(Core.DebugPath, "comp-" + i.ToString() + ".png"), System.Drawing.Imaging.ImageFormat.Png);
}
}
if (withCursor)
g.DrawImageUnscaled(CursorImage, new Point(CursorLocation.X - CaptureRect.Location.X + ShadowOffset.X, CursorLocation.Y - CaptureRect.Location.Y + ShadowOffset.Y));
if (Directory.Exists(Core.DebugPath))
{
Composite.Save(Path.Combine(Core.DebugPath, "final.png"), System.Drawing.Imaging.ImageFormat.Png);
}
}
Trace.WriteLine("Done.", string.Format("ComposedScreenshot.GetComposedScreenshotImage [{0}]", System.Threading.Thread.CurrentThread.Name));
return Composite;
}
private Bitmap GetComposedScreenshotThumbnail()
{
Trace.WriteLine("Creating thumbnail...", string.Format("ComposedScreenshot.GetComposedScreenshotThumbnail [{0}]", System.Threading.Thread.CurrentThread.Name));
float ImageAspectRatio = (float)ComposedScreenshotImage.Width / (float)ComposedScreenshotImage.Height;
float TargetWidth = Math.Min(Screen.PrimaryScreen.WorkingArea.Width / 4, ComposedScreenshotImage.Width);
float TargetHeight = TargetWidth / ImageAspectRatio;
if (TargetHeight >= Screen.PrimaryScreen.WorkingArea.Height-20)
{
//resulting scaled image would be too tall
}
Bitmap result = ComposedScreenshotImage.GetThumbnailImage((int)TargetWidth, (int)TargetHeight, null, IntPtr.Zero) as Bitmap;
Trace.WriteLine("Done.", string.Format("ComposedScreenshot.GetComposedScreenshotThumbnail [{0}]", System.Threading.Thread.CurrentThread.Name));
return result;
}
public void SetOverrideImageData(Bitmap b)
{
_composedScreenshotImage = b;
}
}
}