Working with Layers
Create a Layer Group
A layer group consists of one or more layers and it helps to organize similar or related layers. Using Aspose.PSD for .NET you can create a layer group. For this purpose, a new method AddLayerGroup** **has been added in a **PsdImage **class to adds the layer group**.**
**
The steps to create layer groups are as simple as below:
- Create an instance of an image using the PsdImage class with specified width, height and image options.
- Create a LayerGroup with the specified group name and index.
- Create an instance of the Layer class and assign the PSD image to it.
- Add the created layer to the layer group using the AddLayer method exposed by LayerGroup class
- Save the results.
The following code snippet shows you how to create a layer group.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string inputFile = dataDir + "ButtonTemp.psd"; | |
var createOptions = new PsdOptions(); | |
createOptions.Source = new FileCreateSource(inputFile, false); | |
createOptions.Palette = new PsdColorPalette(new Color[] { Color.Green }); | |
using (var psdImage = (PsdImage)Image.Create(createOptions, 500, 500)) | |
{ | |
LayerGroup group1 = psdImage.AddLayerGroup("Group 1", 0, true); | |
Layer layer1 = new Layer(psdImage); | |
layer1.Name = "Layer 1"; | |
group1.AddLayer(layer1); | |
LayerGroup group2 = group1.AddLayerGroup("Group 2", 1); | |
Layer layer2 = new Layer(psdImage); | |
layer2.Name = "Layer 2"; | |
group2.AddLayer(layer2); | |
Layer layer3 = new Layer(psdImage); | |
layer3.Name = "Layer 3"; | |
group2.AddLayer(layer3); | |
Layer layer4 = new Layer(psdImage); | |
layer4.Name = "Layer 4"; | |
group1.AddLayer(layer4); | |
psdImage.Save(dataDir + "LayerGroups_out.psd"); | |
} |
Rename a Layer
You can use any name that you would like, but the typical practice is to use a general description of the object or element that is on that layer. This article demonstrates how you can change the name of a layer using Aspose.PSD for .NET. For this purpose, a new property DisplayName has been added in Layer class to display a layer name properly. It has been observed that when Photoshop saves a layer name using the Name property, then Korean characters are stored as byte 63'?' in ASCII. So, if you want to display a layer name properly then use the DisplayName property because the Name property does not support Korean characters.
The following code sample shows how you can rename a layer.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_PSD(); | |
// make changes in layer names and save it | |
using (var image = (PsdImage)Image.Load(dataDir + "Korean_layers.psd")) | |
{ | |
for (int i = 0; i < image.Layers.Length; i++) | |
{ | |
var layer = image.Layers[i]; | |
// set new value into DisplayName property | |
layer.DisplayName += "_changed"; | |
} | |
image.Save(dataDir + "Korean_layers_output.psd"); | |
} |
Support of Linked Layers
Linking layers is like grouping the layers. If you are linking two or more layers then It will allow you to make certain changes to both of the linked layers. For example, if you apply transformations to one layer then it will be applied to all other linked layers. This article demonstrates how you can get and unlink linked layers using Aspose.PSD.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
{ | |
Layer[] layers = psd.Layers; | |
// link all layers in one linked group | |
short layersLinkGroupId = psd.LinkedLayersManager.LinkLayers(layers); | |
// gets id for one layer | |
short linkGroupId = psd.LinkedLayersManager.GetLinkGroupId(layers[0]); | |
if (layersLinkGroupId != linkGroupId) | |
{ | |
throw new Exception("layersLinkGroupId and linkGroupId are not equal."); | |
} | |
// gets all linked layers by link group id. | |
Layer[] linkedLayers = psd.LinkedLayersManager.GetLayersByLinkGroupId(linkGroupId); | |
// unlink each layer from group | |
foreach (var linkedLayer in linkedLayers) | |
{ | |
psd.LinkedLayersManager.UnlinkLayer(linkedLayer); | |
} | |
// retrieves NULL for a link group ID that has no layers in the group. | |
linkedLayers = psd.LinkedLayersManager.GetLayersByLinkGroupId(linkGroupId); | |
if (linkedLayers != null) | |
{ | |
throw new Exception("The linkedLayers field is not NULL."); | |
} | |
psd.Save(dataDir + "LinkedLayerexample_output.psd"); | |
} |