Recursive AI

Mirror-pad image edges in C#

OneImage is a free, open-source image library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Mirror padding fills a new border with a reflection of the image instead of a flat color.

Terminal
dotnet add package RecursiveAI.OneImage

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneImage;
Image.Load("tiles.png")
    .Pad(left: 2, top: 1, right: 2,
        bottom: 1, mode: EdgeMode.Mirror)
    .Save("padded.png");
Console.WriteLine("Wrote padded.png");

Unzip the example, then dotnet run.

Text
Wrote padded.png

tiles.png in the download is a 3×2 grid. The top row is red, green, blue. The bottom row is yellow, cyan, magenta.

The full program

C#
using OneImage;
Image.Load("tiles.png")
    .Pad(left: 2, top: 1, right: 2,
        bottom: 1, mode: EdgeMode.Mirror)
    .Save("padded.png");
Console.WriteLine("Wrote padded.png");
 
var saved = Image.Load("padded.png");
Console.WriteLine($"Canvas: {saved.Width} x {saved.Height}");
var row = new Rgba32[saved.Width];
saved.CopyPixelsTo<Rgba32>(row, new ImageRect(0, 0, saved.Width, 1));
Console.WriteLine("Top row: " + string.Join(" ", row.Select(
    p => $"#{p.R:X2}{p.G:X2}{p.B:X2}")));

dotnet run prints:

Text
Wrote padded.png
Canvas: 7 x 4
Top row: #00FF00 #FF0000 #FF0000 #00FF00 #0000FF #0000FF #00FF00

The output top row is green, red, red, green, blue, blue, green. The original top row starts at output coordinate (2,1). Pass only nonnegative border sizes; a negative value throws ArgumentOutOfRangeException.

Variations

Repeat only the edge pixel. Switch to EdgeMode.Clamp to fill the left and right borders with red and blue instead of reflecting green into them. The top row becomes red, red, red, green, blue, blue, blue. Keep using OneImage;.

C#
Image.Load("tiles.png")
    .Pad(left: 2, right: 2, top: 1,
        bottom: 1, mode: EdgeMode.Clamp)
    .Save("clamped.png");

The short answer

Use Pad with mode: EdgeMode.Mirror and specify how many pixels to add on each side. It grows the canvas without scaling the input. OneImage mirrors the pixels at each edge and repeats the edge pixel at the join. With left and top padding, the original top-left pixel moves to (left, top) in the result.

What is mirror padding in image processing?

Mirror padding uses the image itself to fill a larger canvas. Imagine the top row is R G B, and you add two pixels on the left and right. The result is G R | R G B | B G. The bars mark the original row. At the left join, both pixels next to the bar are red; at the right join, both are blue. The second added pixel on either side comes from one position further inside the image. In the vertical direction, the same rule applies to rows. Because the sample adds one row above, the new top row reflects the original first row, including its new side borders.

Is mirror padding the same as symmetric padding?

OneImage’s EdgeMode.Mirror uses a reflection that repeats the boundary sample. This is the convention called symmetric in the NumPy padding examples; the mode NumPy calls reflect excludes that sample at the join. If you’re reproducing another tool’s output, compare what it does to a row of three distinct pixels rather than relying on the mode’s name. With a single pixel added to each side of R G B, OneImage produces R | R G B | B, not G | R G B | G.

Replicate padding is different again: OneImage calls that EdgeMode.Clamp. It repeats the nearest edge pixel for the full width of the border. A one-pixel border can therefore look the same in mirror and clamp modes, but a wider border exposes the difference. Use a constant background instead if the border should be an intentional blank margin rather than an extension of the picture.

All oneimage recipes