Gaussian blur softens detail by blending neighboring pixels.
dotnet add package RecursiveAI.OneImageRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneImage; // input.png is in the zip. Image.Load("input.png") .GaussianBlur(sigma: 3) .Save("blurred.png"); Console.WriteLine("Wrote blurred.png");
Wrote blurred.pngUnzip the example, then dotnet run.
The full program
The example creates a 200 × 140 white image with a blue rectangle, a red stripe, and a 20-pixel border. The pixel construction below defines the complete input, so you can also run it without a sample file.
using OneImage; // Blue rectangle with a red stripe on white. const int width = 200, height = 140; var pixels = new Rgba32[width * height]; var white = new Rgba32(255, 255, 255, 255); var blue = new Rgba32(20, 100, 220, 255); var red = new Rgba32(220, 40, 30, 255); for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) { bool inside = x >= 20 && x < 180 && y >= 20 && y < 120; bool stripe = x >= 90 && x < 110; pixels[y * width + x] = inside ? (stripe ? red : blue) : white; } Image.FromPixels<Rgba32>(pixels, width, height) .Save("input.png"); Image blurred = Image.Load("input.png") .GaussianBlur(sigma: 3); blurred.Save("blurred.png"); Console.WriteLine( $"blurred.png: {blurred.Width}" + $"x{blurred.Height}");
dotnet run prints:
blurred.png: 200x140GaussianBlur builds a lazy operation; Save evaluates it and writes the PNG. Invalid sigma values throw ArgumentOutOfRangeException.
Variations
Horizontal blur. Set a separate vertical sigma to leave that axis unchanged:
using OneImage; Image.Load("input.png") .GaussianBlur(sigma: 6, sigmaY: 0) .Save("horizontal.png"); Console.WriteLine("Wrote horizontal.png");
Wrote horizontal.pngReflect the edges. This uses reflected pixels outside the image instead of repeating its last pixel:
using OneImage; Image.Load("input.png") .GaussianBlur(sigma: 3, edge: EdgeMode.Mirror) .Save("mirror.png"); Console.WriteLine("Wrote mirror.png");
Wrote mirror.pngRelated recipes
The short answer
Load an image with Image.Load, call GaussianBlur(sigma: 3), and save the returned image. The original is not overwritten unless you choose its path as the destination. This operation keeps the image dimensions and applies to every frame, so an animated input is not reduced to its first frame by the blur itself. Choose an output format that supports frames if you need to keep them.
How do I blur an image in C#?
The snippet at the top is a complete file-based example. It reads input.png, softens the full image, and writes blurred.png. You don’t need to construct a convolution kernel or copy pixels into a second buffer yourself. The full program generates a small input with sharp rectangle edges that make the smoothing visible.
A Gaussian blur weights nearby pixels more heavily than distant pixels. Sigma is the standard deviation in pixels, not a percentage and not a target image size. Start with a small sigma, inspect the saved image at its intended display size, and increase it if you need more smoothing. Resizing afterward also changes how large the blur appears on screen.
OneImage defaults to the same sigma on both axes. The horizontal variation sets sigmaY: 0, so it smooths across columns without smoothing between rows. Setting both axes to zero returns the original image unchanged. Negative or nonfinite sigma values are invalid.
How does Gaussian blur handle transparency?
OneImage’s filter documentation specifies linear-light processing on premultiplied alpha by default. Color is multiplied by coverage before neighboring pixels are mixed, and alpha is blurred too. This avoids dark halos from blending an object with the unused color values of transparent pixels.
Use PNG when you need to retain transparency. linear: false switches color mixing to the stored values, but still premultiplies alpha. It does not mean “ignore transparency.”
The default edge mode repeats the boundary pixel. EdgeMode.Mirror, shown above, reflects the image at its boundary. EdgeMode.Constant supplies transparent black outside the image. EdgeMode.Wrap is not supported for this filter.
Is Gaussian blur expensive?
The work depends on both the image and the blur width. OneImage evaluates filters lazily, reading the tiles and neighboring margins it needs when you render or save. The published API describes exact filtering through sigma 16 and a reduced-resolution approximation for larger sigma values. A very wide blur therefore follows a different processing path; it is not simply an arbitrarily large exact kernel.
Choose sigma for the visual result, rather than treating a large value as a quality setting. Gaussian blur is an image effect, not a promise that information has been irreversibly removed. This recipe does not provide privacy redaction.