PSD Layer
Overview
Adobe® Photoshop® PSD Layer is one of the best concepts in graphics processing. Layers contain information about pixels, can have a different count of channels.
One of the most important parts of Layer in Photoshop Document is Layer Resources. You get the full list of supported Layer Resources in PSD from our documentation.
You can find UI to manipulate the layer on the following screenshot:
But Aspose.PSD specializes on programmatic manipulation of PSD Layer via C# and Java.
Additional documentation can be found in this Article: Manipulating Images. All manipulation can be processed to PSD Preview and Layer, you find more info inAspose.PSD Raster Image API Reference.
Available PSD Layer API
- Layer Effects
- Layers common properties
- Layer Metadata
Examples of Layer Editing via C#
Adding of new Layer
If you want to add an empty Layer to opened PSD File you can use the following code.
Add new Layer to PSD File using API
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Make ability to add the newly generated regular layer to PsdImage | |
string sourceFileName = dataDir + "OneLayer.psd"; | |
string exportPath = dataDir + "OneLayerEdited.psd"; | |
string exportPathPng = dataDir + "OneLayerEdited.png"; | |
using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
// Preparing two int arrays | |
var data1 = new int[2500]; | |
var data2 = new int[2500]; | |
var rect1 = new Rectangle(0, 0, 50, 50); | |
var rect2 = new Rectangle(0, 0, 100, 25); | |
for (int i = 0; i < 2500; i++) | |
{ | |
data1[i] = -10000000; | |
data2[i] = -10000000; | |
} | |
var layer1 = im.AddRegularLayer(); | |
layer1.Left = 25; | |
layer1.Top = 25; | |
layer1.Right = 75; | |
layer1.Bottom = 75; | |
layer1.SaveArgb32Pixels(rect1, data1); | |
var layer2 = im.AddRegularLayer(); | |
layer2.Left = 25; | |
layer2.Top = 150; | |
layer2.Right = 125; | |
layer2.Bottom = 175; | |
layer2.SaveArgb32Pixels(rect2, data2); | |
// Save psd | |
im.Save(exportPath, new PsdOptions()); | |
// Save png | |
im.Save(exportPathPng, new PngOptions()); | |
} |
Adding of new Layer from Jpeg, Png, Gif, Ai, Tiff, Bmp files
Files of any supported formats can be added as a new layer to your image. But you can’t load it directly.
You can the code below to add new PSD Layer from the file of any supported format from the stream
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string outputFilePath = dataDir + "PsdResult.psd"; | |
var filesList = new string[] | |
{ | |
"PsdExample.psd", | |
"BmpExample.bmp", | |
"GifExample.gif", | |
"Jpeg2000Example.jpf", | |
"JpegExample.jpg", | |
"PngExample.png", | |
"TiffExample.tif", | |
}; | |
using (var image = new PsdImage(200, 200)) | |
{ | |
foreach (var fileName in filesList) | |
{ | |
string filePath = dataDir + fileName; | |
using (var stream = new FileStream(filePath, FileMode.Open)) | |
{ | |
Layer layer = null; | |
try | |
{ | |
layer = new Layer(stream); | |
image.AddLayer(layer); | |
} | |
catch (Exception e) | |
{ | |
if (layer != null) | |
{ | |
layer.Dispose(); | |
} | |
throw e; | |
} | |
} | |
} | |
image.Save(outputFilePath); | |
} |
Flatten all layers or layer groups
It can be useful if you don’t want to give an editable PSD file to your users. Also, you can identify through API if the file was flattened.
Layer Flattening of PSD File:
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Flatten whole PSD | |
string sourceFileName = dataDir + "ThreeRegularLayersSemiTransparent.psd"; | |
string exportPath = dataDir + "ThreeRegularLayersSemiTransparentFlattened.psd"; | |
using (var im = (PsdImage)(Image.Load(sourceFileName))) | |
{ | |
im.FlattenImage(); | |
im.Save(exportPath); | |
} | |
// Merge one layer in another | |
exportPath = dataDir + "ThreeRegularLayersSemiTransparentFlattenedLayerByLayer.psd"; | |
using (var im = (PsdImage)(Image.Load(sourceFileName))) | |
{ | |
var bottomLayer = im.Layers[0]; | |
var middleLayer = im.Layers[1]; | |
var topLayer = im.Layers[2]; | |
var layer1 = im.MergeLayers(bottomLayer, middleLayer); | |
var layer2 = im.MergeLayers(layer1, topLayer); | |
// Set up merged layers | |
im.Layers = new Layer[] { layer2 }; | |
im.Save(exportPath); | |
} |