diff --git a/IronSoftware.Drawing/IronSoftware.Drawing.Common.Tests/UnitTests/AnyBitmapFunctionality.cs b/IronSoftware.Drawing/IronSoftware.Drawing.Common.Tests/UnitTests/AnyBitmapFunctionality.cs index 91c3322..fe4c6f1 100644 --- a/IronSoftware.Drawing/IronSoftware.Drawing.Common.Tests/UnitTests/AnyBitmapFunctionality.cs +++ b/IronSoftware.Drawing/IronSoftware.Drawing.Common.Tests/UnitTests/AnyBitmapFunctionality.cs @@ -215,6 +215,37 @@ public void AnyBitmap_should_get_Bytes() Assert.Equal(expected, resultExport); } + [FactWithAutomaticDisplayName] + public void AnyBitmap_should_set_Pixel() + { + string imagePath = GetRelativeFilePath("checkmark.jpg"); + + using Image formatRgb24 = Image.Load(imagePath); + using Image formatAbgr32 = Image.Load(imagePath); + using Image formatArgb32 = Image.Load(imagePath); + using Image formatBgr24 = Image.Load(imagePath); + using Image formatBgra32 = Image.Load(imagePath); + + Image[] images = { formatRgb24, formatAbgr32, formatArgb32, formatBgr24, formatBgra32 }; + + foreach (Image image in images) + { + AnyBitmap bitmap = (AnyBitmap)image; + + // Get the current pixel color - should be white + var pixelBefore = bitmap.GetPixel(0, 0); + + // Check current pixel color is not black + Assert.NotEqual(pixelBefore, Color.Black); + + // Set the pixel color to black + bitmap.SetPixel(0, 0, Color.Black); + + // Check the pixel color has changed + Assert.Equal(bitmap.GetPixel(0, 0), Color.Black); + } + } + [FactWithAutomaticDisplayName] public void AnyBitmap_should_get_Stream() { diff --git a/IronSoftware.Drawing/IronSoftware.Drawing.Common/AnyBitmap.cs b/IronSoftware.Drawing/IronSoftware.Drawing.Common/AnyBitmap.cs index 04dfe55..12bfb8c 100644 --- a/IronSoftware.Drawing/IronSoftware.Drawing.Common/AnyBitmap.cs +++ b/IronSoftware.Drawing/IronSoftware.Drawing.Common/AnyBitmap.cs @@ -1009,6 +1009,32 @@ public Color GetPixel(int x, int y) return GetPixelColor(x, y); } + /// + /// Sets the of the specified pixel in this + /// + /// Set in Rgb24 color format. + /// + /// The x-coordinate of the pixel to retrieve. + /// The y-coordinate of the pixel to retrieve. + /// The color to set the pixel. + /// void + public void SetPixel(int x, int y, Color color) + { + if (x < 0 || x >= Width) + { + throw new ArgumentOutOfRangeException(nameof(x), + "x is less than 0, or greater than or equal to Width."); + } + + if (y < 0 || y >= Height) + { + throw new ArgumentOutOfRangeException(nameof(y), + "y is less than 0, or greater than or equal to Height."); + } + + SetPixelColor(x, y, color); + } + /// /// Retrieves the RGB buffer from the image at the specified path. /// @@ -1656,6 +1682,8 @@ public enum ImageFormat /// /// Converts the legacy to and /// + [Obsolete("RotateFlipType is legacy support from System.Drawing. " + + "Please use RotateMode and FlipMode instead.")] internal static (RotateMode, FlipMode) ParseRotateFlipType(RotateFlipType rotateFlipType) { return rotateFlipType switch @@ -2392,6 +2420,31 @@ private Color GetPixelColor(int x, int y) } } + private void SetPixelColor(int x, int y, Color color) + { + switch (Image) + { + case Image imageAsFormat: + imageAsFormat[x, y] = color; + break; + case Image imageAsFormat: + imageAsFormat[x, y] = color; + break; + case Image imageAsFormat: + imageAsFormat[x, y] = color; + break; + case Image imageAsFormat: + imageAsFormat[x, y] = color; + break; + case Image imageAsFormat: + imageAsFormat[x, y] = color; + break; + default: + (Image as Image)[x, y] = color; + break; + } + } + private void LoadAndResizeImage(AnyBitmap original, int width, int height) { #if NET6_0_OR_GREATER