diff --git a/src/ImageSharp/Processing/Extensions/PadExtensions.cs b/src/ImageSharp/Processing/Extensions/PadExtensions.cs
index 29f80b43ff..270380084f 100644
--- a/src/ImageSharp/Processing/Extensions/PadExtensions.cs
+++ b/src/ImageSharp/Processing/Extensions/PadExtensions.cs
@@ -1,7 +1,6 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
-using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing
@@ -20,6 +19,17 @@ public static class PadExtensions
/// The new height.
/// The to allow chaining of operations.
public static IImageProcessingContext Pad(this IImageProcessingContext source, int width, int height)
+ => source.Pad(width, height, default);
+
+ ///
+ /// Evenly pads an image to fit the new dimensions with the given background color.
+ ///
+ /// The source image to pad.
+ /// The new width.
+ /// The new height.
+ /// The background color with which to pad the image.
+ /// The to allow chaining of operations.
+ public static IImageProcessingContext Pad(this IImageProcessingContext source, int width, int height, Color color)
{
var options = new ResizeOptions
{
@@ -28,7 +38,7 @@ public static IImageProcessingContext Pad(this IImageProcessingContext source, i
Sampler = KnownResamplers.NearestNeighbor,
};
- return source.Resize(options);
+ return color.Equals(default) ? source.Resize(options) : source.Resize(options).BackgroundColor(color);
}
}
}
\ No newline at end of file
diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/PadTest.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/PadTest.cs
index 750d67c32a..f568278706 100644
--- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/PadTest.cs
+++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/PadTest.cs
@@ -10,10 +10,10 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public class PadTest
{
public static readonly string[] CommonTestImages =
- {
- TestImages.Png.CalliphoraPartial, TestImages.Png.Bike
- };
-
+ {
+ TestImages.Png.CalliphoraPartial, TestImages.Png.Bike
+ };
+
[Theory]
[WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32)]
public void ImageShouldPad(TestImageProvider provider)
@@ -29,7 +29,30 @@ public void ImageShouldPad(TestImageProvider provider)
{
for (int x = 0; x < 25; x++)
{
- Assert.Equal(default(TPixel), image[x, y]);
+ Assert.Equal(default, image[x, y]);
+ }
+ }
+ }
+ }
+
+ [Theory]
+ [WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32)]
+ public void ImageShouldPadWithBackgroundColor(TestImageProvider provider)
+ where TPixel : struct, IPixel
+ {
+ Color color = Color.Red;
+ TPixel expected = color.ToPixel();
+ using (Image image = provider.GetImage())
+ {
+ image.Mutate(x => x.Pad(image.Width + 50, image.Height + 50, color));
+ image.DebugSave(provider);
+
+ // Check pixels are filled
+ for (int y = 0; y < 25; y++)
+ {
+ for (int x = 0; x < 25; x++)
+ {
+ Assert.Equal(expected, image[x, y]);
}
}
}