Skip to content

Commit

Permalink
#2 - fixing StyleCop issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeclayton committed Feb 23, 2023
1 parent 85df5ad commit ca930fb
Show file tree
Hide file tree
Showing 88 changed files with 771 additions and 870 deletions.
7 changes: 6 additions & 1 deletion src/FancyMouse.PerfTests/Helpers/LayoutHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ public static Rectangle CombineRegions(IList<Rectangle> regions)
{
throw new ArgumentNullException(nameof(regions));
}

if (regions.Count == 0)
{
return Rectangle.Empty;
}

var combined = regions.Aggregate(
seed: regions[0],
func: Rectangle.Union);

return combined;
}

Expand All @@ -34,9 +37,11 @@ public static double GetScalingRatio(Size obj, Size bounds)
{
return 0;
}

var widthRatio = (double)bounds.Width / obj.Width;
var heightRatio = (double)bounds.Height / obj.Height;
var scalingRatio = Math.Min(widthRatio, heightRatio);

return scalingRatio;
}
}
}
13 changes: 4 additions & 9 deletions src/FancyMouse.PerfTests/PerfTests.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using FancyMouse.PerfTests.Helpers;
using System.Diagnostics;
using FancyMouse.PerfTests.Helpers;
using FancyMouse.PerfTests.ScreenCopying;
using FancyMouse.ScreenCopying;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;

namespace FancyMouse.PerfTests
{

[TestClass]
public class PerfTests
{

private static void RunPerfTest(ICopyFromScreen copyHelper)
{
var times = new List<long>();
Expand All @@ -35,7 +33,6 @@ private static void RunPerfTest(ICopyFromScreen copyHelper)
Console.WriteLine("median = {0}", PerfTests.Median(times));
Console.WriteLine("mode = {0}", PerfTests.Mode(times, 50));
Console.WriteLine("times = {0}", string.Join("\r\n", times));

}

[TestMethod]
Expand Down Expand Up @@ -78,11 +75,9 @@ private static long Median(List<long> values)
return values[values.Count / 2];
}

private static long Mode(List<long> values, int bucketSize)
private static long Mode(List<long> values, int bucketSize)
{
return Bucket(values, bucketSize).OrderByDescending(group => group.Count()).First().Key * bucketSize;
}

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ namespace FancyMouse.PerfTests.ScreenCopying;

public sealed class CsWin32JigsawScreenCopyHelper : ICopyFromScreen
{

public Bitmap CopyFromScreen(
Rectangle desktopBounds, IEnumerable<Rectangle> desktopRegions, Size screenshotSize
)
Rectangle desktopBounds, IEnumerable<Rectangle> desktopRegions, Size screenshotSize)
{

/// based on https://www.cyotek.com/blog/capturing-screenshots-using-csharp-and-p-invoke

// based on https://www.cyotek.com/blog/capturing-screenshots-using-csharp-and-p-invoke
var desktopHwnd = HWND.Null;
var desktopHdc = HDC.Null;
var screenshotHdc = CreatedHDC.Null;
Expand All @@ -28,7 +24,6 @@ public Bitmap CopyFromScreen(

try
{

desktopHwnd = PInvoke.GetDesktopWindow();

desktopHdc = PInvoke.GetWindowDC(desktopHwnd);
Expand Down Expand Up @@ -69,25 +64,29 @@ public Bitmap CopyFromScreen(
x: (int)((desktopRegion.X - desktopBounds.X) * scalingRatio),
y: (int)((desktopRegion.Y - desktopBounds.Y) * scalingRatio),
width: (int)(desktopRegion.Width * scalingRatio),
height: (int)(desktopRegion.Height * scalingRatio)
);
height: (int)(desktopRegion.Height * scalingRatio));
apiResult = PInvoke.StretchBlt(
new HDC(screenshotHdc.Value), screenshotRegion.X, screenshotRegion.Y, screenshotRegion.Width, screenshotRegion.Height,
new HDC(desktopHdc.Value), desktopRegion.X, desktopRegion.Y, desktopRegion.Width, desktopRegion.Height,
ROP_CODE.SRCCOPY
);
new HDC(screenshotHdc.Value),
screenshotRegion.X,
screenshotRegion.Y,
screenshotRegion.Width,
screenshotRegion.Height,
new HDC(desktopHdc.Value),
desktopRegion.X,
desktopRegion.Y,
desktopRegion.Width,
desktopRegion.Height,
ROP_CODE.SRCCOPY);
if (apiResult == 0)
{
throw new InvalidOperationException($"{nameof(PInvoke.StretchBlt)} returned {apiResult}");
}
}

screenshot = Bitmap.FromHbitmap(screenshotHBitmap.Value);

}
finally
{

if (!screenshotHdc.IsNull && !originalHBitmap.IsNull)
{
PInvoke.SelectObject(new HDC(screenshotHdc.Value), originalHBitmap);
Expand Down Expand Up @@ -122,7 +121,5 @@ public Bitmap CopyFromScreen(
}

return screenshot ?? throw new InvalidOperationException();

}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using FancyMouse.PerfTests.Helpers;
using System.Drawing.Imaging;
using FancyMouse.PerfTests.Helpers;
using FancyMouse.ScreenCopying;
using System.Drawing.Imaging;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Gdi;
Expand All @@ -9,67 +9,55 @@ namespace FancyMouse.PerfTests.ScreenCopying;

public sealed class ParallelJigsawScreenCopyHelper : ICopyFromScreen
{

public Bitmap CopyFromScreen(
Rectangle desktopBounds, IEnumerable<Rectangle> desktopRegions, Size screenshotSize
)
Rectangle desktopBounds, IEnumerable<Rectangle> desktopRegions, Size screenshotSize)
{

/// based on https://www.cyotek.com/blog/capturing-screenshots-using-csharp-and-p-invoke

// based on https://www.cyotek.com/blog/capturing-screenshots-using-csharp-and-p-invoke
var scalingRatio = LayoutHelper.GetScalingRatio(desktopBounds.Size, screenshotSize);
var parallelOptions = new ParallelOptions
{
MaxDegreeOfParallelism = 1
MaxDegreeOfParallelism = 1,
};

var screenBoundsList = desktopRegions.ToArray();

//var scaledRegions = new Rectangle[screenBoundsList.Length];
//var scaledImages = new Bitmap[screenBoundsList.Length];

var screenshot = new Bitmap(
screenshotSize.Width, screenshotSize.Height, PixelFormat.Format32bppArgb
);
screenshotSize.Width, screenshotSize.Height, PixelFormat.Format32bppArgb);

using var graphics = Graphics.FromImage(screenshot);

Parallel.For(0, screenBoundsList.Length, parallelOptions,
Parallel.For(
0,
screenBoundsList.Length,
parallelOptions,
(i, state) =>
{
var screenRegion = screenBoundsList[i];
var scaledRegion = new Rectangle(
x: (int)((screenRegion.X - desktopBounds.X) * scalingRatio),
y: (int)((screenRegion.Y - desktopBounds.Y) * scalingRatio),
width: (int)(screenRegion.Width * scalingRatio),
height: (int)(screenRegion.Height * scalingRatio)
);
height: (int)(screenRegion.Height * scalingRatio));
using var scaledImage = ParallelJigsawScreenCopyHelper.CopyScreen(desktopBounds, screenRegion, scaledRegion.Size);
graphics.DrawImage(scaledImage, scaledRegion.Location);
}
);
});

return screenshot;

}

private static Bitmap CopyScreen(Rectangle desktopBounds, Rectangle screenBounds, Size scaledSize)
{

var desktopHwnd = HWND.Null;
var desktopHdc = HDC.Null;

var screenshotHdc = CreatedHDC.Null;
var screenshotHBitmap = HBITMAP.Null;
var originalHBitmap = HGDIOBJ.Null;

var screenshot = default(Bitmap);

var apiResult = default(int);

try
{

desktopHwnd = PInvoke.GetDesktopWindow();

desktopHdc = PInvoke.GetWindowDC(desktopHwnd);
Expand All @@ -85,7 +73,7 @@ private static Bitmap CopyScreen(Rectangle desktopBounds, Rectangle screenBounds
}

screenshotHBitmap = PInvoke.CreateCompatibleBitmap(
desktopHdc,
desktopHdc,
scaledSize.Width,
scaledSize.Height);
if (screenshotHBitmap.IsNull)
Expand All @@ -106,21 +94,26 @@ private static Bitmap CopyScreen(Rectangle desktopBounds, Rectangle screenBounds
}

apiResult = PInvoke.StretchBlt(
new HDC(screenshotHdc), 0, 0, scaledSize.Width, scaledSize.Height,
desktopHdc, screenBounds.X, screenBounds.Y, screenBounds.Width, screenBounds.Height,
ROP_CODE.SRCCOPY
);
new HDC(screenshotHdc),
0,
0,
scaledSize.Width,
scaledSize.Height,
desktopHdc,
screenBounds.X,
screenBounds.Y,
screenBounds.Width,
screenBounds.Height,
ROP_CODE.SRCCOPY);
if (apiResult == 0)
{
throw new InvalidOperationException($"{nameof(PInvoke.StretchBlt)} returned {apiResult}");
}

return Bitmap.FromHbitmap(screenshotHBitmap);

}
finally
{

if (!screenshotHdc.IsNull && !originalHBitmap.IsNull)
{
PInvoke.SelectObject(new HDC(screenshotHdc), originalHBitmap);
Expand Down Expand Up @@ -152,9 +145,6 @@ private static Bitmap CopyScreen(Rectangle desktopBounds, Rectangle screenBounds
throw new InvalidOperationException($"{nameof(PInvoke.ReleaseDC)} returned {apiResult}");
}
}

}

}

}
35 changes: 12 additions & 23 deletions src/FancyMouse.PerfTests/ScreenCopying/ScalingScreenCopyHelper.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
using FancyMouse.ScreenCopying;
using System.Drawing.Drawing2D;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using FancyMouse.ScreenCopying;

namespace FancyMouse.PerfTests.ScreenCopying;

public sealed class ScalingScreenCopyHelper : ICopyFromScreen
{

public Bitmap CopyFromScreen(
Rectangle desktopBounds, IEnumerable<Rectangle> desktopRegions, Size screenshotSize
)
Rectangle desktopBounds, IEnumerable<Rectangle> desktopRegions, Size screenshotSize)
{
using var screenshot = (new ScreenScreenCopyHelper()).CopyFromScreen(
desktopBounds, desktopRegions, screenshotSize
);
using var screenshot = new ScreenScreenCopyHelper().CopyFromScreen(
desktopBounds, desktopRegions, screenshotSize);
return ScalingScreenCopyHelper.ResizeImage(
screenshot, screenshotSize
);
screenshot, screenshotSize);
}

private static Bitmap ResizeImage(Image image, Size size)
Expand All @@ -37,29 +33,22 @@ private static Bitmap ResizeImage(Image image, Size size)
private static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);

var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

using (var graphics = Graphics.FromImage(destImage))
{
//// high quality / slow
//graphics.CompositingMode = CompositingMode.SourceCopy;
//graphics.CompositingQuality = CompositingQuality.HighQuality;
//graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
//graphics.SmoothingMode = SmoothingMode.HighQuality;
//graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
// low quality / fast
//graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.InterpolationMode = InterpolationMode.Low;
graphics.SmoothingMode = SmoothingMode.HighSpeed;
graphics.PixelOffsetMode = PixelOffsetMode.HighSpeed;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
using var wrapMode = new ImageAttributes();
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}

return destImage;
}

}
11 changes: 3 additions & 8 deletions src/FancyMouse.PerfTests/ScreenCopying/ScreenScreenCopyHelper.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
using FancyMouse.ScreenCopying;
using System.Drawing.Imaging;
using System.Drawing.Imaging;
using FancyMouse.ScreenCopying;

namespace FancyMouse.PerfTests.ScreenCopying;

public sealed class ScreenScreenCopyHelper : ICopyFromScreen
{

public Bitmap CopyFromScreen(
Rectangle desktopBounds, IEnumerable<Rectangle> desktopRegions, Size screenshotSize
)
Rectangle desktopBounds, IEnumerable<Rectangle> desktopRegions, Size screenshotSize)
{

var screenshot = new Bitmap(desktopBounds.Width, desktopBounds.Height, PixelFormat.Format32bppArgb);
using (var graphics = Graphics.FromImage(screenshot))
{
Expand All @@ -34,7 +31,5 @@ public Bitmap CopyFromScreen(
}

return screenshot;

}

}
Loading

0 comments on commit ca930fb

Please sign in to comment.