Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<Rgb24> formatRgb24 = Image.Load<Rgb24>(imagePath);
using Image<Abgr32> formatAbgr32 = Image.Load<Abgr32>(imagePath);
using Image<Argb32> formatArgb32 = Image.Load<Argb32>(imagePath);
using Image<Bgr24> formatBgr24 = Image.Load<Bgr24>(imagePath);
using Image<Bgra32> formatBgra32 = Image.Load<Bgra32>(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()
{
Expand Down
53 changes: 53 additions & 0 deletions IronSoftware.Drawing/IronSoftware.Drawing.Common/AnyBitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,32 @@ public Color GetPixel(int x, int y)
return GetPixelColor(x, y);
}

/// <summary>
/// Sets the <see cref="Color"/> of the specified pixel in this
/// <see cref="AnyBitmap"/>
/// <para>Set in Rgb24 color format.</para>
/// </summary>
/// <param name="x">The x-coordinate of the pixel to retrieve.</param>
/// <param name="y">The y-coordinate of the pixel to retrieve.</param>
/// <param name="color">The color to set the pixel.</param>
/// <returns>void</returns>
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);
}

/// <summary>
/// Retrieves the RGB buffer from the image at the specified path.
/// </summary>
Expand Down Expand Up @@ -1656,6 +1682,8 @@ public enum ImageFormat
/// <summary>
/// Converts the legacy <see cref="RotateFlipType"/> to <see cref="RotateMode"/> and <see cref="FlipMode"/>
/// </summary>
[Obsolete("RotateFlipType is legacy support from System.Drawing. " +
"Please use RotateMode and FlipMode instead.")]
internal static (RotateMode, FlipMode) ParseRotateFlipType(RotateFlipType rotateFlipType)
{
return rotateFlipType switch
Expand Down Expand Up @@ -2392,6 +2420,31 @@ private Color GetPixelColor(int x, int y)
}
}

private void SetPixelColor(int x, int y, Color color)
{
switch (Image)
{
case Image<Rgb24> imageAsFormat:
imageAsFormat[x, y] = color;
break;
case Image<Abgr32> imageAsFormat:
imageAsFormat[x, y] = color;
break;
case Image<Argb32> imageAsFormat:
imageAsFormat[x, y] = color;
break;
case Image<Bgr24> imageAsFormat:
imageAsFormat[x, y] = color;
break;
case Image<Bgra32> imageAsFormat:
imageAsFormat[x, y] = color;
break;
default:
(Image as Image<Rgba32>)[x, y] = color;
break;
}
}

private void LoadAndResizeImage(AnyBitmap original, int width, int height)
{
#if NET6_0_OR_GREATER
Expand Down