From 9c30bccfe448b1ddd2c16e4617a841348bfc910b Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Fri, 13 Nov 2026 14:31:35 +0100 Subject: [PATCH] Generate Images Using ImageSharp: net11.0, pin ImageSharp 3.1.12 and Drawing 2.1.7, run tests in CI Both projects move to net11.0. The library pins SixLabors.ImageSharp 3.1.12 and SixLabors.ImageSharp.Drawing 2.1.7, the newest versions without a build-time license check. DrawPolygon() gets three points, since the polygon closes itself. The test class drops the Live suffix so CI runs it, and the tests check pixels instead of comparing object references. Test packages updated. --- .../ImageSharpLibrary/ImageService.cs | 2 +- .../ImageSharpLibrary.csproj | 5 +- .../Tests/ImageServiceTests.cs | 84 +++++++++++++++++ .../Tests/ImageSharpLibraryLiveTest.cs | 92 ------------------- .../Tests/Tests.csproj | 10 +- 5 files changed, 93 insertions(+), 100 deletions(-) create mode 100644 csharp-images/GenerateImagesInCSharpUsingImageSharp/Tests/ImageServiceTests.cs delete mode 100644 csharp-images/GenerateImagesInCSharpUsingImageSharp/Tests/ImageSharpLibraryLiveTest.cs diff --git a/csharp-images/GenerateImagesInCSharpUsingImageSharp/ImageSharpLibrary/ImageService.cs b/csharp-images/GenerateImagesInCSharpUsingImageSharp/ImageSharpLibrary/ImageService.cs index 9b0cbd96b8..1c08f70c97 100644 --- a/csharp-images/GenerateImagesInCSharpUsingImageSharp/ImageSharpLibrary/ImageService.cs +++ b/csharp-images/GenerateImagesInCSharpUsingImageSharp/ImageSharpLibrary/ImageService.cs @@ -18,7 +18,7 @@ public static void DrawTriangleOnImage(Image image, PointF point1, Point ValidatePoint(point2); ValidatePoint(point3); var pen = Pens.Solid(Color.Red, 3); - image.Mutate(ctx => ctx.DrawPolygon(pen, [point1, point2, point3, point1])); + image.Mutate(ctx => ctx.DrawPolygon(pen, [point1, point2, point3])); } public static void ValidatePoint(PointF point) diff --git a/csharp-images/GenerateImagesInCSharpUsingImageSharp/ImageSharpLibrary/ImageSharpLibrary.csproj b/csharp-images/GenerateImagesInCSharpUsingImageSharp/ImageSharpLibrary/ImageSharpLibrary.csproj index d132f6a158..90c86a3db4 100644 --- a/csharp-images/GenerateImagesInCSharpUsingImageSharp/ImageSharpLibrary/ImageSharpLibrary.csproj +++ b/csharp-images/GenerateImagesInCSharpUsingImageSharp/ImageSharpLibrary/ImageSharpLibrary.csproj @@ -2,13 +2,14 @@ Exe - net8.0 + net11.0 enable enable - + + diff --git a/csharp-images/GenerateImagesInCSharpUsingImageSharp/Tests/ImageServiceTests.cs b/csharp-images/GenerateImagesInCSharpUsingImageSharp/Tests/ImageServiceTests.cs new file mode 100644 index 0000000000..605560d042 --- /dev/null +++ b/csharp-images/GenerateImagesInCSharpUsingImageSharp/Tests/ImageServiceTests.cs @@ -0,0 +1,84 @@ +using ImageSharpLibrary; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.PixelFormats; + +namespace Tests; + +public class ImageServiceTests +{ + private const string OutputPath = "outputImage.png"; + private const string EmptyOutputPath = ""; + private readonly int _width = 350; + private readonly int _height = 280; + + [Fact] + public void GivenWidthAndHeight_WhenCreatingBlankImage_ThenImageIsCreatedWithGivenDimensions() + { + using var image = ImageService.CreateBlankImage(_width, _height); + + Assert.Equal(_width, image.Width); + Assert.Equal(_height, image.Height); + } + + [Fact] + public void GivenBlankImage_WhenReadingAPixel_ThenPixelIsTransparent() + { + using var image = ImageService.CreateBlankImage(_width, _height); + + Assert.Equal(new Rgba32(0, 0, 0, 0), image[10, 10]); + } + + [Theory] + [InlineData(0, 10)] + [InlineData(10, 0)] + [InlineData(-10, 10)] + [InlineData(10, -10)] + public void GivenInvalidPoint_WhenValidatingPoint_ThenThrowsArgumentException(float x, float y) + { + var point = new PointF(x, y); + + Assert.Throws(() => ImageService.ValidatePoint(point)); + } + + [Fact] + public void GivenValidPoints_WhenDrawingTriangleOnImage_ThenEdgePixelIsRed() + { + using var image = ImageService.CreateBlankImage(_width, _height); + + ImageService.DrawTriangleOnImage(image, new PointF(50, 50), new PointF(250, 50), new PointF(150, 200)); + + Assert.Equal(new Rgba32(255, 0, 0, 255), image[150, 50]); + Assert.Equal(new Rgba32(255, 0, 0, 255), image[100, 125]); + Assert.Equal(new Rgba32(0, 0, 0, 0), image[150, 120]); + } + + [Fact] + public void GivenInvalidPoints_WhenDrawingTriangleOnImage_ThenThrowsArgumentException() + { + using var image = ImageService.CreateBlankImage(_width, _height); + + Assert.Throws(() => + ImageService.DrawTriangleOnImage(image, new PointF(-50, 50), new PointF(150, 50), new PointF(100, 200))); + } + + [Fact] + public void GivenImageAndValidPath_WhenSavingImage_ThenPngFileIsWritten() + { + using var image = ImageService.CreateBlankImage(_width, _height); + + ImageService.SaveImage(image, OutputPath); + + Assert.True(File.Exists(OutputPath)); + using var saved = Image.Load(OutputPath); + Assert.Equal(_width, saved.Width); + File.Delete(OutputPath); + } + + [Fact] + public void GivenEmptyPath_WhenSavingImage_ThenThrowsArgumentException() + { + using var image = ImageService.CreateBlankImage(_width, _height); + + Assert.Throws(() => ImageService.SaveImage(image, EmptyOutputPath)); + } +} diff --git a/csharp-images/GenerateImagesInCSharpUsingImageSharp/Tests/ImageSharpLibraryLiveTest.cs b/csharp-images/GenerateImagesInCSharpUsingImageSharp/Tests/ImageSharpLibraryLiveTest.cs deleted file mode 100644 index 2c2d5c35f6..0000000000 --- a/csharp-images/GenerateImagesInCSharpUsingImageSharp/Tests/ImageSharpLibraryLiveTest.cs +++ /dev/null @@ -1,92 +0,0 @@ -using ImageSharpLibrary; -using SixLabors.ImageSharp.PixelFormats; -using SixLabors.ImageSharp; - -namespace Tests; - -public class ImageSharpLibraryLiveTest -{ - private const string OutputPath = @"outputImage.png"; - private const string NullOutputPath = ""; - private readonly int _width = 350; - private readonly int _height = 280; - - [Fact] - public void GivenWidthAndHeight_WhenCreatingBlankImage_ThenImageIsCreatedWithGivenDimensions() - { - var image = ImageService.CreateBlankImage(_width, _height); - - Assert.Equal(_width, image.Width); - - Assert.Equal(_height, image.Height); - } - - [Theory] - [InlineData(0, 10)] - [InlineData(10, 0)] - [InlineData(-10, 10)] - [InlineData(10, -10)] - public void GivenInvalidPoint_WhenValidatingPoint_ThenThrowsArgumentException(float x, float y) - { - var point = new PointF(x, y); - - Assert.Throws(() => ImageService.ValidatePoint(point)); - } - - [Fact] - public void GivenValidPoints_WhenDrawingTriangleOnImage_ThenDoesNotThrowException() - { - var image = ImageService.CreateBlankImage(_width, _height); - var point1 = new PointF(50, 50); - var point2 = new PointF(250, 50); - var point3 = new PointF(150, 200); - - var ex = Record.Exception(() => ImageService.DrawTriangleOnImage(image, point1, point2, point3)); - - Assert.Null(ex); - } - - [Fact] - public void GivenImageAndValidPath_WhenSavingImage_ThenImageSavedSuccessfully() - { - var image = ImageService.CreateBlankImage(_width, _height); - ImageService.SaveImage(image, OutputPath); - - Assert.True(File.Exists(OutputPath)); - - File.Delete(OutputPath); - } - - [Fact] - public void GivenValidPoints_WhenDrawingTriangleOnImage_ThenImageIsChanged() - { - var point1 = new PointF(50, 50); - var point2 = new PointF(150, 50); - var point3 = new PointF(100, 200); - var image = new Image(_width, _height); - var originalImage = image.Clone(); - - ImageService.DrawTriangleOnImage(image, point1, point2, point3); - - Assert.NotEqual(originalImage, image); - } - - [Fact] - public void GivenInvalidPoints_WhenDrawingTriangleOnImage_ThenThrowsArgumentException() - { - var point1 = new PointF(-50, 50); - var point2 = new PointF(150, 50); - var point3 = new PointF(100, 200); - var image = new Image(_width, _height); - - Assert.Throws(() => ImageService.DrawTriangleOnImage(image, point1, point2, point3)); - } - - [Fact] - public void GivenNullPath_WhenSavingImage_ThenThrowsArgumentNullException() - { - var image = ImageService.CreateBlankImage(_width, _height); - - Assert.Throws(() => ImageService.SaveImage(image, NullOutputPath)); - } -} \ No newline at end of file diff --git a/csharp-images/GenerateImagesInCSharpUsingImageSharp/Tests/Tests.csproj b/csharp-images/GenerateImagesInCSharpUsingImageSharp/Tests/Tests.csproj index 31fa978c87..345bd42f6f 100644 --- a/csharp-images/GenerateImagesInCSharpUsingImageSharp/Tests/Tests.csproj +++ b/csharp-images/GenerateImagesInCSharpUsingImageSharp/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net8.0 + net11.0 enable enable @@ -10,10 +10,10 @@ - - - - + + + +