How to Work with PSD Layers in .NET

How to Work with PSD Layers in .NET

A loaded PsdImage’s Layers property returns its collection of Layer objects. Each Layer exposes editable metadata — name, visibility, opacity, clipping, blend mode, and position (bounds edges) — plus read-only overall bounds and channel information.

Step-by-Step Guide

Step 1: Install the Package

git clone https://github.com/aspose-psd-foss/Aspose.PSD-FOSS-for-.NET.git
cd Aspose.PSD-FOSS-for-.NET
dotnet build

Step 2: Import Required Classes

using System.Linq;
using Aspose.PSD.FileFormats.Psd;
using Aspose.PSD.FileFormats.Core.Blending;

Step 3: Load a Document and List Its Layers

using var image = PsdImage.Load("input.psd");

foreach (var layer in image.Layers)
{
    Console.WriteLine($"{layer.Name}: {layer.Width}x{layer.Height} at ({layer.Left},{layer.Top})");
}

Step 4: Read and Edit Layer Visibility, Opacity, and Blend Mode

IsVisible, Opacity (0-255), Clipping, Name, and BlendModeKey all have working setters:

var layer = image.Layers[0];

layer.IsVisible = false;          // hide the layer
layer.Opacity = 200;              // ~78% opacity
layer.BlendModeKey = BlendMode.Multiply;
layer.Name = "Renamed Layer";

Step 5: Read and Reposition Layer Bounds

Bounds, Width, Height, and ChannelsCount are read-only — but Top, Left, Bottom, and Right each have a working setter that repositions the layer:

Console.WriteLine($"Bounds: {layer.Bounds}");
Console.WriteLine($"Channels: {layer.ChannelsCount}");

layer.Left = 10;
layer.Top = 20;
layer.Right = 40;
layer.Bottom = 60;

Step 6: Replace the Layers Collection

PsdImage.Layers itself has a working setter, so you can replace the whole collection (for example, to remove a layer):

image.Layers = image.Layers.Where(l => l.Name != "Renamed Layer").ToArray();

Step 7: Save the Document

image.Save("output.psd");

Common Issues and Fixes

NotSupportedException when setting ChannelInformation, LayerMaskData, or LayerBlendingRangesData. These three Layer setters are not supported in this FOSS build — treat them as read-only.

Opacity value looks wrong after setting it. Opacity is a byte (0-255), not a 0.0-1.0 fraction or a percentage — layer.Opacity = 200 means roughly 78% opaque (200/255).

Changes to a layer don’t appear after saving. Confirm you are modifying the Layer instance obtained from image.Layers, and that you call image.Save after making your edits.

Frequently Asked Questions

Can I remove a layer from a document?

Yes — PsdImage.Layers has a working setter, so you can assign it a filtered array to remove layers (see Step 6).

What blend modes are available?

BlendMode includes values such as Normal, Multiply, Screen, Overlay, and Darken, among others — set via Layer.BlendModeKey.

Is layer mask data available?

Layer.LayerMaskData has a working getter, but its setter throws NotSupportedException in this FOSS build.

See Also