Working with Group Layers

Usually PSD files consist of a huge number of different layers, which that can make the Layers panel messy and cluttered. A group layer is a special type of layer from PSD files that organize related layers together. Group layers can contain not only related layers but also nested group layers. Graphics Mill supports manipulation with group layers from PSD files.

You can access layers from PSD files in two ways:

Hierarchical Access to Layers

The PsdReader.GroupedFrames property returns layers from the top level. The returned collection consists of outer nodes, such as raster and text layers, and layer groups that contain child nodes. So you can extract any layer recursively. For example, we have a PSD file with the layer structure illustrated on the picture below from the left. Graphics Mill represents this file with the structure shown on the following picture from the right:

Group layer example.

The following code snippet reads a PSD file, iterates through its all hierarchical levels recursively, and saves bitmaps from the raster and text layers with a name as "<containing group layers>-<layer's name >.png":

C#
static private string _destFolder = @"Images\Layers\";

static private void ExtractGroup(GroupedFrameCollection groupFrame, string groupName)
{
    foreach (var frame in groupFrame)
    {
        if (frame.Type == FrameType.Group)
            ExtractGroup(((PsdGroupFrame)frame).Frames, groupName + "-" + frame.Name);

        else
            SaveLayer(frame, groupName);
    }
}

static private void SaveLayer(PsdFrame frame, string groupName)
{
    if (frame.Type == FrameType.Raster || frame.Type == FrameType.Text)
        using (var writer = new PngWriter(GetSafePath(_destFolder + groupName + "-" + frame.Name + ".png")))
            Pipeline.Run(frame + writer);

    else
        throw new ApplicationException(frame.Name);
}

static public void LoadMain()
{
    using (var reader = new PsdReader(@"Images\BusinessCard.psd"))
        ExtractGroup(reader.GroupedFrames, "Root");
}

Layer names can contain symbols that are not allowed in the names of files. Therefore the function GetSafePath(string) converts the name of a layer to an appropriate file name:

C#
static private string GetSafePath(string path)
{
    string[] str = path.Split(System.IO.Path.GetInvalidPathChars());
    for (int j = 0; j < str.Length; j++)
    {
        if (j == 0)
        {
            path = str[0];
            continue;
        }

        path = path + str[j];
    }

    return path;
}

As a result the picture below illustrates the folder's content which shows the hierarchical structure of layers from the PSD file :

Work with GroupedFrames example.

Flat Access to Layers

The PsdReader.Frames property returns all raster and text layers from a PSD file simultaneously and does not separate them into groups. But you can recover group layers for any layer using the PsdFrame.Parent property that returns a reference to the PsdFrame object if the layer belongs to any group, and null otherwise.

The following code performs the same task as the previous one but it uses the PsdReader.Frames property instead PsdReader.GroupedFrames:

C#
using (var reader = new PsdReader(@"Images\BusinessCard.psd"))
{
    var frames = reader.Frames;

    foreach (var frame in frames)
    {

        if (frame.Type == FrameType.Raster || frame.Type == FrameType.Text)
        {
            string path = "";

            var parent = frame.Parent;

            while (parent != null)
            {
                path += parent.Name + "-";
                parent = parent.Parent;
            }

            using (var writer = new PngWriter(_destFolder + "root-" + path + frame.Name + ".png"))
                Pipeline.Run(frame + writer);
        }
    }
}

The result of this snippet is shown on the previous picture.

See Also

Reference

Manual