forked from toehead2001/aeroshot
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Screenshot.cs
1073 lines (915 loc) · 44.4 KB
/
Screenshot.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* AeroShot - Transparent screenshot utility for Windows
Copyright (C) 2021 Cvolton
Copyright (C) 2015 toe_head2001
Copyright (C) 2012 Caleb Joseph
AeroShot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
AeroShot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace AeroShot
{
internal struct ScreenshotTask
{
public bool CaptureMouse;
public bool DisableClearType;
public bool CustomGlass;
public Color AeroColor;
public bool ClipboardNotDisk;
public string DiskSaveDirectory;
public bool DoResize;
public int ResizeX;
public int ResizeY;
public bool DoCanvas;
public int CanvasX;
public int CanvasY;
public bool DisableShadow;
public IntPtr WindowHandle;
public bool SaveActiveDark;
public bool SaveActiveLight;
public bool SaveActiveTransparent;
public bool SaveInactiveDark;
public bool SaveInactiveLight;
public bool SaveInactiveTransparent;
public bool CropMode;
public bool SaveMask;
public ScreenshotTask(IntPtr window, bool clipboard, string file,
bool resize, int resizeX, int resizeY,
bool customGlass, Color aeroColor,
bool mouse, bool clearType, bool shadow,
bool saveActiveDark, bool saveActiveLight, bool saveInactiveDark,
bool saveInactiveLight, bool saveMask, bool saveActiveTransparent,
bool saveInactiveTransparent,
bool canvas, int canvasX, int canvasY,
bool cropMode)
{
WindowHandle = window;
ClipboardNotDisk = clipboard;
DiskSaveDirectory = file;
DoResize = resize;
ResizeX = resizeX;
ResizeY = resizeY;
DoCanvas = canvas;
CanvasX = canvasX;
CanvasY = canvasY;
CustomGlass = customGlass;
AeroColor = aeroColor;
CaptureMouse = mouse;
DisableClearType = clearType;
DisableShadow = shadow;
SaveActiveDark = saveActiveDark;
SaveActiveLight = saveActiveLight;
SaveActiveTransparent = saveActiveTransparent;
SaveInactiveDark = saveInactiveDark;
SaveInactiveLight = saveInactiveLight;
SaveInactiveTransparent = saveInactiveTransparent;
SaveMask = saveMask;
CropMode = cropMode;
}
}
internal static class Screenshot
{
private const uint SWP_NOACTIVATE = 0x0010;
private const int GWL_STYLE = -16;
private const long WS_SIZEBOX = 0x00040000L;
private const uint SWP_SHOWWINDOW = 0x0040;
private const uint SPI_GETFONTSMOOTHING = 0x004A;
private const uint SPI_GETFONTSMOOTHINGTYPE = 0x200A;
private const uint SPI_SETFONTSMOOTHINGTYPE = 0x200B;
private const uint FE_FONTSMOOTHINGCLEARTYPE = 0x2;
private const uint FE_FONTSMOOTHINGSTANDARD = 0x1;
private const uint SPIF_UPDATEINIFILE = 0x1;
private const uint SPIF_SENDCHANGE = 0x2;
private static uint SPI_SETDROPSHADOW = 0x1025;
private const uint RDW_FRAME = 0x0400;
private const uint RDW_INVALIDATE = 0x0001;
private const uint RDW_UPDATENOW = 0x0100;
private const uint RDW_ALLCHILDREN = 0x0080;
internal static void CaptureWindow(ref ScreenshotTask data)
{
IntPtr start = WindowsApi.FindWindow("Button", "Start");
IntPtr taskbar = WindowsApi.FindWindow("Shell_TrayWnd", null);
if (data.ClipboardNotDisk || Directory.Exists(data.DiskSaveDirectory))
{
try
{
// Hide the taskbar, just incase it gets in the way
if (data.WindowHandle != start &&
data.WindowHandle != taskbar)
{
WindowsApi.ShowWindow(start, 0);
WindowsApi.ShowWindow(taskbar, 0);
Application.DoEvents();
}
bool ClearTypeToggled = false;
if (data.DisableClearType && ClearTypeEnabled())
{
WindowsApi.SystemParametersInfo(SPI_SETFONTSMOOTHINGTYPE, 0, FE_FONTSMOOTHINGSTANDARD, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
WindowsApi.RedrawWindow(data.WindowHandle, IntPtr.Zero, IntPtr.Zero, RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN);
ClearTypeToggled = true;
}
WindowsApi.SetForegroundWindow(data.WindowHandle);
bool AeroColorToggled = false;
uint ColorizationColor = 0;
bool ColorizationOpaqueBlend = false;
WindowsApi.DWM_COLORIZATION_PARAMS originalParameters = new WindowsApi.DWM_COLORIZATION_PARAMS();
if (Environment.OSVersion.Version.Major >= 6)
{
try
{
if (data.CustomGlass && AeroEnabled())
{
if (VersionHelpers.IsWindowsVista())
{
WindowsApi.DwmGetColorizationColor(out ColorizationColor, out ColorizationOpaqueBlend);
WindowsApi.DwmpSetColorization(ColorToBgra(data.AeroColor), true, 0xFF);
}
else
{
// Original colorization parameters
WindowsApi.DwmGetColorizationParameters(out originalParameters);
// Custom colorization parameters
WindowsApi.DWM_COLORIZATION_PARAMS parameters;
WindowsApi.DwmGetColorizationParameters(out parameters);
parameters.clrAfterGlowBalance = 2;
parameters.clrBlurBalance = 29;
parameters.clrColor = ColorToBgra(data.AeroColor);
parameters.nIntensity = 69;
// Call the DwmSetColorizationParameters to make the change take effect.
WindowsApi.DwmSetColorizationParameters(ref parameters, false);
}
AeroColorToggled = true;
}
}
catch (Exception)
{
AeroColorToggled = false; //workaround for Vista Betas
}
}
bool ShadowToggled = false;
if (data.DisableShadow && ShadowEnabled())
{
WindowsApi.SystemParametersInfo(SPI_SETDROPSHADOW, 0, false, 0);
ShadowToggled = true;
}
var r = new WindowsRect(0);
if (data.DoResize)
{
SmartResizeWindow(ref data, out r);
Thread.Sleep(100);
}
int length = WindowsApi.GetWindowTextLength(data.WindowHandle);
var sb = new StringBuilder(length + 1);
WindowsApi.GetWindowText(data.WindowHandle, sb, sb.Capacity);
string name = sb.ToString();
foreach (char inv in Path.GetInvalidFileNameChars())
name = name.Replace(inv.ToString(), string.Empty);
Bitmap[] s = CaptureCompositeScreenshot(ref data);
if (AeroColorToggled && Environment.OSVersion.Version.Major >= 6)
{
if (VersionHelpers.IsWindowsVista())
{
WindowsApi.DwmpSetColorization(ColorizationColor, ColorizationOpaqueBlend, 0xFF);
}
else
WindowsApi.DwmSetColorizationParameters(ref originalParameters, false);
}
if (ClearTypeToggled)
{
WindowsApi.SystemParametersInfo(SPI_SETFONTSMOOTHINGTYPE, 0, FE_FONTSMOOTHINGCLEARTYPE, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
WindowsApi.RedrawWindow(data.WindowHandle, IntPtr.Zero, IntPtr.Zero, RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN);
}
if (ShadowToggled)
{
WindowsApi.SystemParametersInfo(SPI_SETDROPSHADOW, 0, true, 0);
}
// Show the taskbar again
if (data.WindowHandle != start &&
data.WindowHandle != taskbar)
{
WindowsApi.ShowWindow(start, 1);
WindowsApi.ShowWindow(taskbar, 1);
}
if (s == null || s[0] == null)
{
MessageBox.Show("The screenshot taken was blank, it will not be saved.",
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if (data.ClipboardNotDisk)
{
var whiteS = new Bitmap(s[0].Width, s[0].Height, PixelFormat.Format24bppRgb);
using (Graphics graphics = Graphics.FromImage(whiteS))
{
graphics.Clear(Color.White);
graphics.DrawImage(s[0], 0, 0, s[0].Width, s[0].Height);
}
using (var pngStream = new MemoryStream())
using (var dibStream = new MemoryStream())
{
// Save screenshot in clipboard as PNG which some applications support (eg. Microsoft Office)
s[0].Save(pngStream, ImageFormat.Png);
var pngClipboardData = new DataObject("PNG", pngStream);
Byte[] dibData = DIBConverter.ConvertToDib(s[0]);
dibStream.Write(dibData, 0, dibData.Length);
pngClipboardData.SetData(DataFormats.Dib, false, dibStream);
//var pngClipboardData = new DataObject(DataFormats.Dib, false, dibStream);
// Add fallback for applications that don't support PNG from clipboard (eg. Photoshop or Paint)
//pngClipboardData.SetData(DataFormats.Bitmap, s[0]);
Clipboard.Clear();
Clipboard.SetDataObject(pngClipboardData, true);
}
whiteS.Dispose();
}
else
{
name = name.Trim();
if (name == string.Empty)
name = "AeroShot";
string pathActive = Path.Combine(data.DiskSaveDirectory, name + "_b1.png");
string pathInactive = Path.Combine(data.DiskSaveDirectory, name + "_b2.png");
string pathWhiteActive = Path.Combine(data.DiskSaveDirectory, name + "_w1.png");
string pathWhiteInactive = Path.Combine(data.DiskSaveDirectory, name + "_w2.png");
string pathMask = Path.Combine(data.DiskSaveDirectory, name + "_mask.png");
string pathTransparentActive = Path.Combine(data.DiskSaveDirectory, name + "_t1.png");
string pathTransparentInactive = Path.Combine(data.DiskSaveDirectory, name + "_t2.png");
if (File.Exists(pathActive) || File.Exists(pathInactive) || File.Exists(pathWhiteActive) || File.Exists(pathWhiteInactive) || File.Exists(pathMask) || File.Exists(pathTransparentActive) || File.Exists(pathTransparentInactive))
{
for (int i = 1; i < 9999; i++)
{
pathActive = Path.Combine(data.DiskSaveDirectory, name + " " + i + "_b1.png");
pathInactive = Path.Combine(data.DiskSaveDirectory, name + " " + i + "_b2.png");
pathWhiteActive = Path.Combine(data.DiskSaveDirectory, name + " " + i + "_w1.png");
pathWhiteInactive = Path.Combine(data.DiskSaveDirectory, name + " " + i + "_w2.png");
pathMask = Path.Combine(data.DiskSaveDirectory, name + " " + i + "_mask.png");
pathTransparentActive = Path.Combine(data.DiskSaveDirectory, name + " " + i + "_t1.png");
pathTransparentInactive = Path.Combine(data.DiskSaveDirectory, name + " " + i + "_t2.png");
if (!File.Exists(pathActive) && !File.Exists(pathInactive) && !File.Exists(pathWhiteActive) && !File.Exists(pathWhiteInactive) && !File.Exists(pathMask) && !File.Exists(pathTransparentActive) && !File.Exists(pathTransparentInactive))
break;
}
}
if (data.SaveActiveDark)
s[0].Save(pathActive, ImageFormat.Png);
if (data.SaveInactiveDark)
s[1].Save(pathInactive, ImageFormat.Png);
if (data.SaveActiveLight)
s[2].Save(pathWhiteActive, ImageFormat.Png);
if (data.SaveInactiveLight)
s[3].Save(pathWhiteInactive, ImageFormat.Png);
if (data.SaveMask)
s[4].Save(pathMask, ImageFormat.Png);
if (data.SaveActiveTransparent)
s[5].Save(pathTransparentActive, ImageFormat.Png);
if (data.SaveInactiveTransparent)
s[6].Save(pathTransparentInactive, ImageFormat.Png);
}
if (s[0] != null)
s[0].Dispose();
if (s[1] != null)
s[1].Dispose();
if (s[2] != null)
s[2].Dispose();
if (s[3] != null)
s[3].Dispose();
if (s[4] != null)
s[4].Dispose();
}
if (data.DoResize)
{
if ((WindowsApi.GetWindowLong(data.WindowHandle, GWL_STYLE) & WS_SIZEBOX) == WS_SIZEBOX)
{
WindowsApi.SetWindowPos(data.WindowHandle, (IntPtr)0, r.Left, r.Top, r.Right - r.Left, r.Bottom - r.Top, SWP_SHOWWINDOW);
}
}
}
catch (Exception e)
{
if (data.WindowHandle != start && data.WindowHandle != taskbar)
{
WindowsApi.ShowWindow(start, 1);
WindowsApi.ShowWindow(taskbar, 1);
}
MessageBox.Show("An error occurred while trying to take a screenshot.\r\n\r\nPlease make sure you have selected a valid window.\r\n\r\n" + e.ToString(),
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Invalid directory chosen.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private static bool AeroEnabled()
{
bool aeroEnabled = true;
WindowsApi.DwmIsCompositionEnabled(out aeroEnabled);
return aeroEnabled;
}
private static bool ShadowEnabled()
{
return System.Windows.SystemParameters.DropShadow;
}
// Helper method to convert from a .NET color to a Win32 BGRA-format color.
private static uint ColorToBgra(Color color)
{
return (uint)(color.B | (color.G << 8) | (color.R << 16) | (color.A << 24));
}
private static bool ClearTypeEnabled()
{
int sv = 0;
/* Call to systemparametersinfo to get the font smoothing value. */
WindowsApi.SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, ref sv, 0);
int stv = 0;
/* Call to systemparametersinfo to get the font smoothing Type value. */
WindowsApi.SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE, 0, ref stv, 0);
if (sv > 0 && stv == 2) //if smoothing is on, and is set to cleartype
{
return true;
}
else
{
return false;
}
}
private static void SmartResizeWindow(ref ScreenshotTask data, out WindowsRect oldWindowSize)
{
oldWindowSize = new WindowsRect(0);
if ((WindowsApi.GetWindowLong(data.WindowHandle, GWL_STYLE) & WS_SIZEBOX) != WS_SIZEBOX)
return;
var r = new WindowsRect();
WindowsApi.GetWindowRect(data.WindowHandle, ref r);
oldWindowSize = r;
Bitmap f = CaptureCompositeScreenshot(ref data)[0];
if (f != null)
{
WindowsApi.SetWindowPos(data.WindowHandle, (IntPtr)0, r.Left, r.Top, data.ResizeX - (f.Width - (r.Right - r.Left)), data.ResizeY - (f.Height - (r.Bottom - r.Top)), SWP_SHOWWINDOW);
f.Dispose();
}
else
{
WindowsApi.SetWindowPos(data.WindowHandle, (IntPtr)0, r.Left, r.Top, data.ResizeX, data.ResizeY, SWP_SHOWWINDOW);
}
}
private static void RefreshBackdrop()
{
Application.DoEvents();
Thread.Sleep(33); //Waiting for DWM to refresh, assuming 30 Hz refresh rate to be safe
}
private static unsafe Bitmap[] CaptureCompositeScreenshot(ref ScreenshotTask data)
{
// Generate a rectangle with the size of all monitors combined
Rectangle totalSize = Rectangle.Empty;
foreach (Screen s in Screen.AllScreens)
totalSize = Rectangle.Union(totalSize, s.Bounds);
var rct = new WindowsRect();
if (WindowsApi.DwmGetWindowAttribute(data.WindowHandle, DwmWindowAttribute.ExtendedFrameBounds, ref rct, sizeof(WindowsRect)) != 0)
// DwmGetWindowAttribute() failed, usually means Aero is disabled so we fall back to GetWindowRect()
WindowsApi.GetWindowRect(data.WindowHandle, ref rct);
else
{
// DwmGetWindowAttribute() succeeded
}
// Get DPI of the window (this only works properly on Win 10 1607+) but it is not really needed until Win 11 anyway
int DPI = 96;
try
{
DPI = WindowsApi.GetDpiForWindow(data.WindowHandle);
}
catch { }
// Adjust margin for DPI
double scalingFactor = DPI / 96;
int backdropOffset = Convert.ToInt32(100 * scalingFactor);
// Add a 100px margin for window shadows. Excess transparency is trimmed out later
rct.Left -= backdropOffset;
rct.Right += backdropOffset;
rct.Top -= backdropOffset;
rct.Bottom += backdropOffset;
// These next 4 checks handle if the window is outside of the visible screen
if (rct.Left < totalSize.Left)
rct.Left = totalSize.Left;
if (rct.Right > totalSize.Right)
rct.Right = totalSize.Right;
if (rct.Top < totalSize.Top)
rct.Top = totalSize.Top;
if (rct.Bottom > totalSize.Bottom)
rct.Bottom = totalSize.Bottom;
// Spawning backdrop
// Handling as much as possible in the constructor makes this easier to render, which makes capture less likely to fail on underpowered PCs
Color tmpColor = Color.White;
var backdrop = new Form
{
BackColor = tmpColor,
FormBorderStyle = FormBorderStyle.None,
ShowInTaskbar = false,
//Opacity = 0,
Size = new Size(rct.Right - rct.Left, rct.Bottom - rct.Top),
StartPosition = FormStartPosition.Manual,
Location = new Point(rct.Left, rct.Top)
};
WindowsApi.ShowWindow(backdrop.Handle, 4);
if (!WindowsApi.SetWindowPos(backdrop.Handle, data.WindowHandle, rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top, SWP_NOACTIVATE))
{
// We are unable to put backdrop directly behind the window, so we will put it into the foreground and then put the original window on top of it
// This likely happens because the program we're trying to capture is running as administrator
WindowsApi.SetWindowPos(backdrop.Handle, IntPtr.Zero, rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top, SWP_NOACTIVATE);
WindowsApi.SetForegroundWindow(data.WindowHandle).ToString();
}
RefreshBackdrop();
// Capture screenshot with white background
Bitmap whiteShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top));
backdrop.BackColor = Color.Black;
RefreshBackdrop();
// Capture screenshot with black background
Bitmap blackShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top));
Bitmap transparentImage;
Bitmap transparentInactiveImage = null;
Bitmap transparentWhiteImage = null;
Bitmap transparentWhiteInactiveImage = null;
Bitmap transparentMaskImage = null;
Bitmap transparentTransparentImage = null;
Bitmap transparentTransparentInactiveImage = null;
transparentImage = DifferentiateAlpha(whiteShot, blackShot, false);
if (data.SaveActiveLight) transparentWhiteImage = DifferentiateAlpha(whiteShot, blackShot, true);
whiteShot.Dispose();
blackShot.Dispose();
//Capture black mask screenshot
if (data.SaveMask)
{
int minAlpha = 0;
bool isCompositing = false;
bool ShadowToggled = false;
bool ColorToggled = false;
try
{
WindowsApi.DwmIsCompositionEnabled(out isCompositing);
}
catch (Exception)
{
//OS doesn't have a supported version of DWM
}
//We can't disable shadows on Vista without disabling DWM, which would cause the mask to be inaccurate
UInt32 ColorizationColor = 0;
bool fOpaque = true;
if (isCompositing && (VersionHelpers.IsWindowsVista() || VersionHelpers.IsWindows11()))
{
minAlpha = 254;
WindowsApi.DwmGetColorizationColor(out ColorizationColor, out fOpaque);
if (fOpaque == false)
{
WindowsApi.DwmpSetColorization(ColorizationColor, true, 0xFF);
ColorToggled = true;
}
}
else if(ShadowEnabled())
{
WindowsApi.SystemParametersInfo(SPI_SETDROPSHADOW, 0, false, 0);
ShadowToggled = true;
}
backdrop.BackColor = Color.White;
RefreshBackdrop();
Bitmap whiteMaskShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top));
backdrop.BackColor = Color.Black;
RefreshBackdrop();
Bitmap blackMaskShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top));
transparentMaskImage = CreateMask(DifferentiateAlpha(whiteMaskShot, blackMaskShot, false), minAlpha);
if (ShadowToggled)
{
WindowsApi.SystemParametersInfo(SPI_SETDROPSHADOW, 0, true, 0);
}
if (ColorToggled)
{
WindowsApi.DwmpSetColorization(ColorizationColor, fOpaque, 0xFF);
}
whiteMaskShot.Dispose();
blackMaskShot.Dispose();
}
//Capture active fully transparent
if (data.SaveActiveTransparent)
{
try
{
//win 7
WindowsApi.DWM_COLORIZATION_PARAMS parameters, originalParameters = new WindowsApi.DWM_COLORIZATION_PARAMS();
//win vista
UInt32 ColorizationColor = 0;
bool fOpaque = true;
if (VersionHelpers.IsWindowsVista())
{
WindowsApi.DwmGetColorizationColor(out ColorizationColor, out fOpaque);
if (fOpaque == false)
{
WindowsApi.DwmpSetColorization(0xFFFFFF, false, 0xFF);
}
}
else
{
WindowsApi.DwmGetColorizationParameters(out parameters);
WindowsApi.DwmGetColorizationParameters(out originalParameters);
//Set custom fully transparent parameters
parameters.clrAfterGlowBalance = 0;
parameters.clrBlurBalance = 100;
parameters.nIntensity = 0;
// Call the DwmSetColorizationParameters to make the change take effect.
WindowsApi.DwmSetColorizationParameters(ref parameters, false);
}
backdrop.BackColor = Color.White;
RefreshBackdrop();
Bitmap whiteTransparentShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top));
backdrop.BackColor = Color.Black;
RefreshBackdrop();
Bitmap blackTransparentShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top));
transparentTransparentImage = DifferentiateAlpha(whiteTransparentShot, blackTransparentShot, false);
whiteTransparentShot.Dispose();
blackTransparentShot.Dispose();
if (VersionHelpers.IsWindowsVista())
{
WindowsApi.DwmpSetColorization(ColorizationColor, fOpaque, 0xFF);
}
else
{
WindowsApi.DwmSetColorizationParameters(ref originalParameters, false);
}
}
catch (Exception)
{
transparentTransparentImage = new Bitmap(transparentImage);
}
}
//Show form to steal focus
var emptyForm = new Form
{
FormBorderStyle = FormBorderStyle.None,
ShowInTaskbar = false,
Opacity = 0,
};
WindowsApi.ShowWindow(emptyForm.Handle, 5);
WindowsApi.SetWindowPos(emptyForm.Handle, data.WindowHandle, rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top, 0);
WindowsApi.SetForegroundWindow(emptyForm.Handle);
// Capture inactive screenshots
if (data.SaveInactiveDark || data.SaveInactiveLight)
{
backdrop.BackColor = Color.White;
RefreshBackdrop();
// Capture inactive screenshot with white background
Bitmap whiteInactiveShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top));
backdrop.BackColor = Color.Black;
RefreshBackdrop();
// Capture inactive screenshot with black background
Bitmap blackInactiveShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top));
if (data.SaveInactiveDark) transparentInactiveImage = DifferentiateAlpha(whiteInactiveShot, blackInactiveShot, false);
if (data.SaveInactiveLight) transparentWhiteInactiveImage = DifferentiateAlpha(whiteInactiveShot, blackInactiveShot, true);
whiteInactiveShot.Dispose();
blackInactiveShot.Dispose();
}
//Capture inactive fully transparent
if (data.SaveInactiveTransparent)
{
try
{
//Get original colorization parameters
WindowsApi.DWM_COLORIZATION_PARAMS parameters, originalParameters;
WindowsApi.DwmGetColorizationParameters(out parameters);
WindowsApi.DwmGetColorizationParameters(out originalParameters);
//Set custom fully transparent parameters
parameters.clrAfterGlowBalance = 0;
parameters.clrBlurBalance = 100;
parameters.nIntensity = 0;
// Call the DwmSetColorizationParameters to make the change take effect.
WindowsApi.DwmSetColorizationParameters(ref parameters, false);
backdrop.BackColor = Color.White;
RefreshBackdrop();
Bitmap whiteTransparentInactiveShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top));
backdrop.BackColor = Color.Black;
RefreshBackdrop();
Bitmap blackTransparentInactiveShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top));
transparentTransparentInactiveImage = DifferentiateAlpha(whiteTransparentInactiveShot, blackTransparentInactiveShot, false);
whiteTransparentInactiveShot.Dispose();
blackTransparentInactiveShot.Dispose();
WindowsApi.DwmSetColorizationParameters(ref originalParameters, false);
}
catch (Exception)
{
transparentTransparentInactiveImage = new Bitmap(transparentInactiveImage);
}
}
backdrop.Dispose();
emptyForm.Dispose();
if (data.CaptureMouse)
DrawCursorToBitmap(transparentImage, new Point(rct.Left, rct.Top));
Bitmap[] final = CropEmptyEdges(new[] { transparentImage, transparentInactiveImage, transparentWhiteImage, transparentWhiteInactiveImage, transparentMaskImage, transparentTransparentImage, transparentTransparentInactiveImage }, Color.FromArgb(0, 0, 0, 0), ref data);
// Returns a bitmap with transparency, calculated by differentiating the white and black screenshots
return final;
}
private static void DrawCursorToBitmap(Bitmap windowImage, Point offsetLocation)
{
var ci = new CursorInfoStruct();
ci.cbSize = Marshal.SizeOf(ci);
if (WindowsApi.GetCursorInfo(out ci))
{
if (ci.flags == 1)
{
IntPtr hicon = WindowsApi.CopyIcon(ci.hCursor);
IconInfoStruct icInfo;
if (WindowsApi.GetIconInfo(hicon, out icInfo))
{
var loc = new Point(ci.ptScreenPos.X - offsetLocation.X - icInfo.xHotspot, ci.ptScreenPos.Y - offsetLocation.Y - icInfo.yHotspot);
Icon ic = Icon.FromHandle(hicon);
Bitmap bmp = ic.ToBitmap();
Graphics g = Graphics.FromImage(windowImage);
g.DrawImage(bmp, new Rectangle(loc, bmp.Size));
g.Dispose();
WindowsApi.DestroyIcon(hicon);
bmp.Dispose();
}
}
}
}
private static Bitmap CaptureScreenRegion(Rectangle crop)
{
Rectangle totalSize = Rectangle.Empty;
foreach (Screen s in Screen.AllScreens)
totalSize = Rectangle.Union(totalSize, s.Bounds);
IntPtr hSrc = WindowsApi.CreateDC("DISPLAY", null, null, 0);
IntPtr hDest = WindowsApi.CreateCompatibleDC(hSrc);
IntPtr hBmp = WindowsApi.CreateCompatibleBitmap(hSrc, crop.Right - crop.Left, crop.Bottom - crop.Top);
IntPtr hOldBmp = WindowsApi.SelectObject(hDest, hBmp);
WindowsApi.BitBlt(hDest, 0, 0, crop.Right - crop.Left, crop.Bottom - crop.Top, hSrc, crop.Left, crop.Top, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
Bitmap bmp = Image.FromHbitmap(hBmp);
WindowsApi.SelectObject(hDest, hOldBmp);
WindowsApi.DeleteObject(hBmp);
WindowsApi.DeleteDC(hDest);
WindowsApi.DeleteDC(hSrc);
return bmp.Clone(new Rectangle(0, 0, bmp.Width, bmp.Height), PixelFormat.Format24bppRgb);
}
private static unsafe Bitmap GenerateChecker(int s)
{
var b1 = new Bitmap(s * 2, s * 2, PixelFormat.Format24bppRgb);
var b = new UnsafeBitmap(b1);
b.LockImage();
PixelData* pixel;
for (int x = 0, y = 0; x < s * 2 && y < s * 2;)
{
pixel = b.GetPixel(x, y);
if ((x >= 0 && x <= s - 1) && (y >= 0 && y <= s - 1))
pixel->SetAll(255);
if ((x >= s && x <= s * 2 - 1) && (y >= 0 && y <= s - 1))
pixel->SetAll(200);
if ((x >= 0 && x <= s - 1) && (y >= s && y <= s * 2 - 1))
pixel->SetAll(200);
if ((x >= s && x <= s * 2 - 1) && (y >= s && y <= s * 2 - 1))
pixel->SetAll(255);
if (x == s * 2 - 1)
{
y++;
x = 0;
continue;
}
x++;
}
b.UnlockImage();
return b1;
}
private static unsafe Bitmap[] CropEmptyEdges(Bitmap[] b1, Color trimColor, ref ScreenshotTask data)
{
if (b1 == null)
return null;
int sizeX = b1[0].Width;
int sizeY = b1[0].Height;
var b = new UnsafeBitmap(b1[0]);
b.LockImage();
int left = -1;
int top = -1;
int right = -1;
int bottom = -1;
PixelData* pixel;
for (int x = 0, y = 0; ;)
{
pixel = b.GetPixel(x, y);
if (left == -1)
{
if ((trimColor.A == 0 && pixel->Alpha != 0) || (trimColor.R != pixel->Red & trimColor.G != pixel->Green & trimColor.B != pixel->Blue))
{
left = x;
x = 0;
y = 0;
continue;
}
if (y == sizeY - 1)
{
x++;
y = 0;
}
else
y++;
continue;
}
if (top == -1)
{
if ((trimColor.A == 0 && pixel->Alpha != 0) || (trimColor.R != pixel->Red & trimColor.G != pixel->Green & trimColor.B != pixel->Blue))
{
top = y;
x = sizeX - 1;
y = 0;
continue;
}
if (x == sizeX - 1)
{
y++;
x = 0;
}
else
x++;
continue;
}
if (right == -1)
{
if ((trimColor.A == 0 && pixel->Alpha != 0) || (trimColor.R != pixel->Red & trimColor.G != pixel->Green & trimColor.B != pixel->Blue))
{
right = x + 1;
x = 0;
y = sizeY - 1;
continue;
}
if (y == sizeY - 1)
{
x--;
y = 0;
}
else
y++;
continue;
}
if (bottom == -1)
{
if ((trimColor.A == 0 && pixel->Alpha != 0) || (trimColor.R != pixel->Red & trimColor.G != pixel->Green & trimColor.B != pixel->Blue))
{
bottom = y + 1;
break;
}
if (x == sizeX - 1)
{
y--;
x = 0;
}
else
x++;
continue;
}
}
b.UnlockImage();
if (left >= right || top >= bottom)
return null;
int rightSize = right - left;
int bottomSize = bottom - top;
if (rightSize >= b1[0].Width || bottomSize >= b1[0].Height)
MessageBox.Show("Background removal very likely failed, please check the final screenshot after capture.");
if (data.CropMode) //if crop mode: keep window centered
{
if (b1[0].Width - rightSize - left > left)
rightSize = b1[0].Width - (2 * left);
else
{
int oldLeft = left;
left = b1[0].Width - rightSize - left;
rightSize += (oldLeft - left);
}
if (b1[0].Height - bottomSize - top > top)
bottomSize = b1[0].Height - (2 * top);
else
{
int oldTop = top;
top = b1[0].Height - bottomSize - top;
bottomSize += (oldTop - top);
}
}
int canvasRightSize = rightSize % 2 == 0 ? rightSize : rightSize + 1;
int canvasBottomSize = bottomSize % 2 == 0 ? bottomSize : bottomSize + 1;
if (data.DoCanvas)
{
canvasRightSize = data.CanvasX;
canvasBottomSize = data.CanvasY;
}
Bitmap[] final = new Bitmap[b1.Length];
for(int i = 0; i < b1.Length; i++)
{
if (b1[i] == null)
continue;
final[i] = b1[i].Clone(new Rectangle(left, top, rightSize, bottomSize), b1[i].PixelFormat);
Bitmap temp = new Bitmap(canvasRightSize, canvasBottomSize);
using (Graphics grD = Graphics.FromImage(temp))
{
grD.DrawImage(final[i], new Rectangle(0, 0, final[i].Width, final[i].Height), new Rectangle(0, 0, final[i].Width, final[i].Height), GraphicsUnit.Pixel);
}
final[i].Dispose();
final[i] = temp;
b1[i].Dispose();
}
return final;
}
private static unsafe Bitmap DifferentiateAlpha(Bitmap whiteBitmap, Bitmap blackBitmap, bool useWhiteMath)
{
if (whiteBitmap == null || blackBitmap == null || whiteBitmap.Width != blackBitmap.Width || whiteBitmap.Height != blackBitmap.Height)
return null;
int sizeX = whiteBitmap.Width;
int sizeY = whiteBitmap.Height;
var final = new Bitmap(sizeX, sizeY, PixelFormat.Format32bppArgb);
var a = new UnsafeBitmap(whiteBitmap);
var b = new UnsafeBitmap(blackBitmap);
var f = new UnsafeBitmap(final);
a.LockImage();
b.LockImage();
f.LockImage();
bool empty = true;
for (int x = 0, y = 0; x < sizeX && y < sizeY;)
{
PixelData* pixelA = a.GetPixel(x, y);
PixelData* pixelB = b.GetPixel(x, y);
PixelData* pixelF = f.GetPixel(x, y);
pixelF->Alpha = ToByte((pixelB->Red - pixelA->Red + 255 + pixelB->Green - pixelA->Green + 255 + pixelB->Blue - pixelA->Blue + 255) / 3);
if (pixelF->Alpha > 0)
{
if (useWhiteMath)
{
// Following math creates an image optimized to be displayed on a white background
pixelF->Red = ToByte(255 * (pixelA->Red + pixelF->Alpha - 255) / pixelF->Alpha);
pixelF->Green = ToByte(255 * (pixelA->Green + pixelF->Alpha - 255) / pixelF->Alpha);
pixelF->Blue = ToByte(255 * (pixelA->Blue + pixelF->Alpha - 255) / pixelF->Alpha);
}
else
{