Add New Layers in PSD using C#

PSD (Photoshop Document) is a native image file format used by Adobe’s popular Photoshop Application. It is commonly used to create logos, brochures, and other images where the PSD file contains several layers. In certain cases, we may need to manipulate the PSD files programmatically. In this article, we will learn how to add a new layer in a PSD using C#.

The following topics shall be covered in this article:

  1. C# API to Add New Layer in PSD
  2. Add a New Layer to PSD
  3. Add a New Text Layer to PSD
  4. Create a Layer from Image in PSD

C# API to Add New Layer in PSD

To create a new layer in a PSD file, we will be using the Aspose.PSD for .NET API. It supports creating, editing, or manipulating PSD and several other supported file formats.

The PsdImage class of the API allows loading, editing, and saving PSD files. The AddRegularLayer() method of this class adds a new regular layer. Whereas, the AddTextLayer(string, Rectangle) method adds a new Text layer to the PSD. This class also provides the Save(string) method that saves the PSD to the specified file location.

Please either download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.PSD

Add New Layer to PSD using C#

We can add a new layer to a PSD file by following the steps given below:

  1. Firstly, load the PSD file as PsdImage using the Image class.
  2. Next, prepare an object to add such as Rectangle.
  3. Then, call the AddRegularLayer() method.
  4. Optionally, set the layer position such as Top, Left, Right, and Bottom.
  5. After that, assign the object to the layer using the SaveArgb32Pixels() method.
  6. Finally, save the output file using the Save() method.

The following code sample shows how to add a new layer to a PSD file in C#.

Add-New-Layer-to-PSD-Programmatically-using-CSharp

Add New Layer to PSD Programmatically using C#

Add New Text Layer to PSD using C#

We can add a new text layer to a PSD file by following the steps given below:

  1. Firstly, load the PSD file as PsdImage using the Image class.
  2. Next, define a Rectangle.
  3. Then, call the AddTexLayer() method. It takes text input and a rectangle object as arguments.
  4. After that, set the layer position such as Top, Left, Right, and Bottom.
  5. Finally, save the output file using the Save() method.

The following code sample shows how to add a new text layer to a PSD file in C#.

Add-New-Text-Layer-to-PSD-using-CSharp

Add New Text Layer to PSD Programmatically using C#

Create New Layer from Image in PSD using C#

We can also add a new layer from a PNG or JPG image to a PSD file by following the steps given below:

  1. Firstly, load the PSD file as PsdImage using the Image class.
  2. Next, open the PNG image file in a stream.
  3. Then, create an instance of the Layer class with the stream object.
  4. After that, call the AddLayer() method. It takes the Layer object as an argument.
  5. Finally, save the output file using the Save() method.

The following code sample shows how to add a new layer from a PNG image to a PSD file in C#.

Create-New-Layer-from-Image-in-PSD-using-CSharp

Create New Layer from Image in PSD using C#

Get Free Temporary License

You can get a free temporary license to try Aspose.PSD for .NET without evaluation limitations.

Conclusion

In this article, we have learned how to:

  • create a new PSD image;
  • create a new regular layer and a text layer in a PSD image;
  • insert a PNG or JPG image as a new layer in PSD using C#.

Besides, you can learn more about Aspose.PSD for .NET using the documentation and explore different features supported by the API. In case of any ambiguity, please feel free to contact us on our forum.

See Also