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:
- C# API to Add New Layer in PSD
- Add a New Layer to PSD
- Add a New Text Layer to PSD
- 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:
- Firstly, load the PSD file as PsdImage using the Image class.
- Next, prepare an object to add such as Rectangle.
- Then, call the AddRegularLayer() method.
- Optionally, set the layer position such as Top, Left, Right, and Bottom.
- After that, assign the object to the layer using the SaveArgb32Pixels() method.
- 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 Text Layer to PSD using C#
We can add a new text layer to a PSD file by following the steps given below:
- Firstly, load the PSD file as PsdImage using the Image class.
- Next, define a Rectangle.
- Then, call the AddTexLayer() method. It takes text input and a rectangle object as arguments.
- After that, set the layer position such as Top, Left, Right, and Bottom.
- 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#.
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:
- Firstly, load the PSD file as PsdImage using the Image class.
- Next, open the PNG image file in a stream.
- Then, create an instance of the Layer class with the stream object.
- After that, call the AddLayer() method. It takes the Layer object as an argument.
- 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#.
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.