Search code examples
c#imagemagicklayerpsdpaint.net

How to get layer relation or group info in a psd file?


I'm trying to get layer information from a psd file, in a C# project.

By using the PSD Plugin for Paint.Net or MagickImage, I'm able to get the position, size, content and some visual properties of each layer.

But I can't find properties or functions which can tell the relationship of the layers.

Is there a way to find out which layer is parent of another? or which layer is in same group of another?


Solution

  • Using PSD plugin for Paint.Net, Layer relationship can be found from the AdditionalInfo of each Layer.

    1. PSDFile.Layers contains ordered list of Layers (reverse order compared with PhotoShop)
    2. Foreach Layer, if it has a LayerSectionInfo in its AdditionalInfo property, then it is an important node for recreate the layer tree.
    3. Find out the LayerSectionType of Layer.AdditionalInfo[index of LayerSectionInfo].SectionType.
      • If it is OpenFolder or ClosedFolder, then it is a parent layer and a open tag, layers after it are its children;
      • if it is SectionDivider, then it is a close tag layer indicate end of current layer group;
      • if LayerSectionInfo does not exist, then it is a common layer node.

    for example, layers look like this:

    (OF for OpenFolder, L(SD) for Layer which is SectionDivider)

    [A(OF), A0, A1(OF), A10, L(SD), L(SD), B(OF), B0(OF), B00, L(SD), B1, L(SD)]

    can be translate to this layer tree:

    • A
      • A0
      • A1
        • A10
    • B
      • B0
        • B00
      • B1