-
Notifications
You must be signed in to change notification settings - Fork 12
/
Screenshot.cs
665 lines (587 loc) · 26.9 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
/* AeroShot - Transparent screenshot utility for Windows
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 System;
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 enum BackgroundType
{
Transparent,
Checkerboard,
SolidColor
}
public BackgroundType Background;
public Color BackgroundColor;
public bool CaptureMouse;
public bool DisableClearType;
public int CheckerboardSize;
public bool CustomGlass;
public Color AeroColor;
public bool ClipboardNotDisk;
public string DiskSaveDirectory;
public bool DoResize;
public int ResizeX;
public int ResizeY;
public bool DisableShadow;
public IntPtr WindowHandle;
public ScreenshotTask(IntPtr window, bool clipboard, string file,
bool resize, int resizeX, int resizeY,
BackgroundType backType, Color backColor,
int checkerSize, bool customGlass, Color aeroColor,
bool mouse, bool clearType, bool shadow)
{
WindowHandle = window;
ClipboardNotDisk = clipboard;
DiskSaveDirectory = file;
DoResize = resize;
ResizeX = resizeX;
ResizeY = resizeY;
Background = backType;
BackgroundColor = backColor;
CheckerboardSize = checkerSize;
CustomGlass = customGlass;
AeroColor = aeroColor;
CaptureMouse = mouse;
DisableClearType = clearType;
DisableShadow = shadow;
}
}
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;
WindowsApi.DWM_COLORIZATION_PARAMS originalParameters;
WindowsApi.DwmGetColorizationParameters(out originalParameters);
if (data.CustomGlass && AeroEnabled())
{
// Original colorization parameters
originalParameters.clrGlassReflectionIntensity = 50;
// 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;
}
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)
{
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)
{
MessageBox.Show("The screenshot taken was blank, it will not be saved.",
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if (data.ClipboardNotDisk && data.Background !=
ScreenshotTask.BackgroundType.Transparent)
// Screenshot is already opaque, don't need to modify it
Clipboard.SetImage(s);
else if (data.ClipboardNotDisk)
{
var whiteS = new Bitmap(s.Width, s.Height, PixelFormat.Format24bppRgb);
using (Graphics graphics = Graphics.FromImage(whiteS))
{
graphics.Clear(Color.White);
graphics.DrawImage(s, 0, 0, s.Width, s.Height);
}
using (var stream = new MemoryStream())
{
// Save screenshot in clipboard as PNG which some applications support (eg. Microsoft Office)
s.Save(stream, ImageFormat.Png);
var pngClipboardData = new DataObject("PNG", stream);
// Add fallback for applications that don't support PNG from clipboard (eg. Photoshop or Paint)
pngClipboardData.SetData(DataFormats.Bitmap, whiteS);
Clipboard.Clear();
Clipboard.SetDataObject(pngClipboardData, true);
}
whiteS.Dispose();
}
else
{
name = name.Trim();
if (name == string.Empty)
name = "AeroShot";
string path = Path.Combine(data.DiskSaveDirectory, name + ".png");
if (File.Exists(path))
{
for (int i = 1; i < 9999; i++)
{
path = Path.Combine(data.DiskSaveDirectory, name + " " + i + ".png");
if (!File.Exists(path))
break;
}
}
s.Save(path, ImageFormat.Png);
}
s.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)
{
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.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Invalid directory chosen.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private static bool AeroEnabled()
{
bool aeroEnabled;
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);
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 unsafe Bitmap CaptureCompositeScreenshot(ref ScreenshotTask data)
{
Color tmpColor = data.BackgroundColor;
if (data.Background == ScreenshotTask.BackgroundType.Transparent ||
data.Background == ScreenshotTask.BackgroundType.Checkerboard)
tmpColor = Color.White;
var backdrop = new Form
{
BackColor = tmpColor,
FormBorderStyle = FormBorderStyle.None,
ShowInTaskbar = false,
Opacity = 0
};
// 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
// Add a 100px margin for window shadows. Excess transparency is trimmed out later
rct.Left -= 100;
rct.Right += 100;
rct.Top -= 100;
rct.Bottom += 100;
}
// 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;
WindowsApi.ShowWindow(backdrop.Handle, 4);
WindowsApi.SetWindowPos(backdrop.Handle, data.WindowHandle, rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top, SWP_NOACTIVATE);
backdrop.Opacity = 1;
Application.DoEvents();
// Capture screenshot with white background
Bitmap whiteShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top));
if (data.Background == ScreenshotTask.BackgroundType.SolidColor)
{
backdrop.Dispose();
if (data.CaptureMouse)
DrawCursorToBitmap(whiteShot, new Point(rct.Left, rct.Top));
Bitmap final = CropEmptyEdges(whiteShot, tmpColor);
whiteShot.Dispose();
return final;
}
backdrop.BackColor = Color.Black;
Application.DoEvents();
// Capture screenshot with black background
Bitmap blackShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top));
backdrop.Dispose();
Bitmap transparentImage = DifferentiateAlpha(whiteShot, blackShot);
if (data.CaptureMouse)
DrawCursorToBitmap(transparentImage, new Point(rct.Left, rct.Top));
transparentImage = CropEmptyEdges(transparentImage, Color.FromArgb(0, 0, 0, 0));
whiteShot.Dispose();
blackShot.Dispose();
if (data.Background == ScreenshotTask.BackgroundType.Checkerboard)
{
var final = new Bitmap(transparentImage.Width, transparentImage.Height, PixelFormat.Format24bppRgb);
Graphics finalGraphics = Graphics.FromImage(final);
var brush = new TextureBrush(GenerateChecker(data.CheckerboardSize));
finalGraphics.FillRectangle(brush, finalGraphics.ClipBounds);
finalGraphics.DrawImageUnscaled(transparentImage, 0, 0);
finalGraphics.Dispose();
transparentImage.Dispose();
return final;
}
// Returns a bitmap with transparency, calculated by differentiating the white and black screenshots
return transparentImage;
}
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)
{
if (b1 == null)
return null;
int sizeX = b1.Width;
int sizeY = b1.Height;
var b = new UnsafeBitmap(b1);
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;
Bitmap final = b1.Clone(new Rectangle(left, top, right - left, bottom - top), b1.PixelFormat);
b1.Dispose();
return final;
}
private static unsafe Bitmap DifferentiateAlpha(Bitmap whiteBitmap, Bitmap blackBitmap)
{
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)
{
// Following math creates an image optimized to be displayed on a black background
pixelF->Red = ToByte(255 * pixelB->Red / pixelF->Alpha);
pixelF->Green = ToByte(255 * pixelB->Green / pixelF->Alpha);
pixelF->Blue = ToByte(255 * pixelB->Blue / pixelF->Alpha);
// 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);
*/
}
if (empty && pixelF->Alpha > 0)
empty = false;
if (x == sizeX - 1)
{
y++;
x = 0;
continue;
}
x++;
}
a.UnlockImage();
b.UnlockImage();
f.UnlockImage();
return empty ? null : final;
}
private static byte ToByte(int i)
{
return (byte)(i > 255 ? 255 : (i < 0 ? 0 : i));
}
}
}