Recursive AI

Read a Photoshop layer mask in C#

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

A layer mask controls which parts of a layer show. Try it on badge.psd from the zip.

Terminal
dotnet add package RecursiveAI.OneImage.Psd

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

C#
using OneImage.Psd;
using var psd = PsdFile.Load("badge.psd");
var layer = psd.Document.Root.Children[0];
if (layer.Mask is { } mask)
    mask.Pixels.Snapshot().Save("mask.png");

Unzip the example, then dotnet run.

Writes mask.png, a white circle on black, without printing anything.

The full program

The sample has one blue layer named Badge with a circular raster mask. The document is 64 × 48 pixels. The mask is smaller and starts at document position (14, 11). The program looks up the layer by name and reports that position alongside the exported image.

C#
using OneImage.Psd;
 
using var file = PsdFile.Load("badge.psd");
var layer = file.Document.Root.Children
    .FirstOrDefault(item => item.Name == "Badge");
if (layer?.Mask is not { } mask)
{
    Console.WriteLine("No mask on the Badge layer.");
    return;
}
 
var pixels = mask.Pixels.Snapshot();
pixels.Save("mask.png");
Console.WriteLine($"Layer: {layer.Name}");
Console.WriteLine($"Mask: {pixels.Width} x {pixels.Height} at ({mask.X}, {mask.Y})");
Console.WriteLine($"Outside: {mask.OutsideValue}; enabled: {mask.Enabled}");
Console.WriteLine("Wrote mask.png");
Text
Layer: Badge
Mask: 32 x 24 at (14, 11)
Outside: 0; enabled: True
Wrote mask.png

If the named layer is missing or has no mask, the program prints No mask on the Badge layer. and writes nothing.

To find a nested layer, start at Document.Root.Children, compare each layer’s Name, and recursively search Children on each GroupLayer you encounter.

Variations

To see the mask applied instead of exporting it by itself, add this line at the end of the full program. It renders the document and writes preview.png.

C#
file.Document.Render().Save("preview.png");

The short answer

Open the layered file with PsdFile.Load, select a layer, and read Layer.Mask. When it’s present, mask.Pixels is an 8-bit grayscale canvas. Take a Snapshot() to export it as PNG. Keep mask.X, mask.Y, and mask.OutsideValue if you’ll need to put those pixels back in the right place. A flattened PNG of the document contains the result of masking, not the mask itself.

How do I view a Photoshop layer mask by itself?

Open mask.png from the snippet, not a render of the document. White (255) lets the layer show, black (0) hides it, and intermediate gray lets it show partly. Those values describe how much of the layer’s own coverage to keep, not what color to paint. In badge.psd, the circle is white in the mask, but it’s blue in the rendered document because the source layer is blue.

The mask has its own dimensions. Pixel (0, 0) in the exported PNG belongs at (mask.X, mask.Y) in document coordinates, not necessarily at the top left of the layer or the document. Outside the mask’s bounds the library uses OutsideValue; in the example it’s zero. A PNG of the mask rectangle does not store either that outside value or the document offset.

What’s the difference between a raster mask and a vector mask in a PSD?

A raster mask is a grid of coverage values; a vector mask defines coverage with a path. The PSD reader exposes vector masks as rasterized coverage, using the non-zero fill rule. If both kinds are present, the reader combines their coverage by multiplication. So Layer.Mask.Pixels can show the combined result; it isn’t an API for extracting two independent masks or recovering editable vector paths.

How do I tell if a Photoshop layer mask is disabled?

Inspect mask.Enabled rather than looking for a black or white image. A disabled mask can still have pixels you can export; the compositor ignores the mask while it’s disabled. That’s also different from layer.Mask being null, which means the layer has no mask in the document model at all.

For an enabled mask, Density can weaken its effect and Feather can soften its edges when the document is rendered. The raw grayscale canvas is useful if you need to inspect the mask values; render the document if you want to see what the settings, mask, and layer pixels produce together.

All oneimage recipes