Recursive AI

Add a drop shadow to a transparent PNG in C#

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

Terminal
dotnet add package RecursiveAI.OneImage

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

C#
using OneImage;
using OneImage.Layers;
using var doc = new Document(96, 96);
var layer = doc.CreatePixelLayer("logo",
    Image.Load("logo.png"), 24, 24);
doc.Add(layer).Style = new LayerStyle(
    [new DropShadowEffect()]);
doc.Render().Save("shadow.png");

Unzip the example, then dotnet run.

Writes shadow.png, with a shadow that follows the logo’s outline. The zip includes logo.png, the 48 × 48 transparent logo shown below.

Blue L-shaped mark on a transparent background

The full program

The zip gives the shadow a longer offset and a softer edge. It reads the same pixel immediately to the right of the blue mark before adding the style and again from the saved PNG. Alpha is opacity on a scale from 0 (clear) to 255 (opaque).

C#
using OneImage;
using OneImage.Layers;
 
Image logo = Image.Load("logo.png");
using var doc = new Document(96, 96);
var layer = doc.Add(doc.CreatePixelLayer(
    "logo", logo, x: 24, y: 24));
 
// The mark ends at x = 63. Sample one pixel to its right.
var sample = new ImageRect(64, 58, 1, 1);
var pixel = new Rgba32[1];
doc.Render().CopyPixelsTo<Rgba32>(pixel, sample);
Console.WriteLine($"Alpha at (64, 58) before: {pixel[0].A}");
 
layer.Style = new LayerStyle([
    new DropShadowEffect { Angle = 135, Distance = 8, Size = 10 }
]);
doc.Render().Save("shadow.png");
Image.Load("shadow.png").CopyPixelsTo<Rgba32>(pixel, sample);
Console.WriteLine($"Alpha at (64, 58) after: {pixel[0].A}");
Console.WriteLine("Wrote shadow.png");
Text
Alpha at (64, 58) before: 0
Alpha at (64, 58) after: 67
Wrote shadow.png

The rendered shadow.png:

Blue L-shaped logo with a soft shadow below and to the right on a transparent background

If the shadow is cut off, give the document more room. Setting Size above 250 throws ArgumentOutOfRangeException instead of increasing the blur.

Variations

Cast a colored shadow straight down. Replace the style assignment in the full program. Angle = 90 puts the light directly above the logo, so the shadow falls below it.

C#
layer.Style = new LayerStyle([
    new DropShadowEffect
    {
        Angle = 90, Distance = 6, Size = 8,
        Color = new Rgba32(40, 70, 140, 255)
    }
]);

The short answer

Load the PNG into a pixel layer, set layer.Style to a LayerStyle containing a DropShadowEffect, then render the document and save it as PNG. The document starts transparent. Make it larger than the logo and put the logo inside its edges so there’s room for the shadow. To change how far it extends, adjust Distance for the offset and Size for the blur before rendering.

How do I add a shadow to a PNG image?

Start with a PNG where the background is already transparent. The renderer uses the layer’s alpha (and its mask, if there is one) to decide which parts cast the shadow. With the sample logo, that shape is the blue L, not the transparent 48 × 48 square that contains it. If you start with a PNG flattened onto white, the white pixels are opaque too: the shadow follows the whole rectangle. Adding this style doesn’t remove a background.

The shadow is drawn below the logo. Distance offsets it away from the light, Spread grows the shape before the blur, and Size controls the blur. Set Opacity when the shadow looks too dark without changing the logo’s own opacity. The defaults are black at 75% opacity, with a distance and size of 5 pixels. The layer styles documentation lists the rest of the settings.

How do I keep transparency in a PNG with a drop shadow?

Keep the document background transparent and save the render as .png. The space beyond the logo and its shadow can stay fully transparent; the soft edge contains pixels that are only partly opaque. In the example, the pixel next to the mark changes from alpha 0 to 67. It lets the background show through instead of putting a solid gray patch there. Saving to JPEG can’t retain that transparency.

A transparent background doesn’t let the render grow beyond the document. The sample positions a 48 × 48 image at (24, 24) in a 96 × 96 document, leaving space on every side. For your own logo, leave room in the direction the shadow falls and around the blurred edge. If you increase the distance or blur, inspect the output edges again before cropping away empty space.

The saved PNG is a single rendered image, not an editable shadow layer. To change the shadow later without working from pixels that already include it, keep the original logo and render it again with the new settings.

All oneimage recipes