How to save each layer as png?

Hi, Support:
If a psd file have many layers, how to save each layer as png file with transparent background and original layer width x height, and name the png file with its layer name?

Thanks for your help.

@ducaisoft

Can you please explain your requirements in the form of source file and desired output that we may test on our end to help you further.

please refer to the demo.
Test.zip (6.5 MB)

@ducaisoft

We need to investigate this further on our end. A ticket with ID PSDNET-915 has been created in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be notified once the issue will be addressed.

@ducaisoft

Please use following code to export each layer to PNG.

   string src = "InputPSDImage.psd";

            void SaveLayers(string dirPath, Layer[] layers)
            {
                Directory.CreateDirectory(dirPath);

                for (int i = 0; i < layers.Length; i++)
                {
                    var layer = layers[i];
                    if (layer is SectionDividerLayer)
                    {
                        var layerGroup = ((SectionDividerLayer)layer).GetRelatedLayerGroup();
                        string folderPath = Path.Combine(dirPath, layerGroup.Name);
                        SaveLayers(folderPath, layerGroup.Layers);

                        i += layerGroup.Layers.Length + 1;
                        continue;
                    }

                    string output = Path.Combine(dirPath, layer.DisplayName + ".png");
                    layer.Save(output, new PngOptions() { ColorType = PngColorType.TruecolorWithAlpha });
                }
            };

            using (PsdImage image = (PsdImage)Aspose.PSD.Image.Load(src))
            {
                // How to save each layer as a separate PNG file.
                var layers = image.Layers;
                string folderPath = Path.Combine(Path.GetDirectoryName(src), Path.GetFileNameWithoutExtension(src));
                SaveLayers(folderPath, layers);

                // How to get the count of layers and know the layers name.
                List<string> layerNames = new List<string>();
                int totalLayersCount = layers.Length;
                int folderSectionsCount = 0;
                int layersCount = 0;

                foreach (var layer in layers)
                {
                    if (layer is LayerGroup || layer is SectionDividerLayer)
                    {
                        folderSectionsCount++;
                        continue;
                    }

                    layerNames.Add(layer.DisplayName);
                }
                layersCount = totalLayersCount - folderSectionsCount;
            }

Thanks for your solution.
However, I cannot convert it into VB.net.
Would you rewrite it based on VB.net?
And which Aspose.psd subclass must be imported?
and which version of the Aspose.PSD.dll for net will support this function?

Thanks.

@ducaisoft

Please try using the following converted sample code using latest Aspose.PSD for .NET 21.7.

Friend Class SurroundingClass
    Private src As String = "InputPSDImage.psd"

    Private Sub SaveLayers(ByVal dirPath As String, ByVal layers As Layer())
        Directory.CreateDirectory(dirPath)

        For i As Integer = 0 To layers.Length - 1
            Dim layer = layers(i)

            If TypeOf layer Is SectionDividerLayer Then
                Dim layerGroup = CType(layer, SectionDividerLayer).GetRelatedLayerGroup()
                Dim folderPath As String = Path.Combine(dirPath, layerGroup.Name)
                SaveLayers(folderPath, layerGroup.Layers)
                i += layerGroup.Layers.Length + 1
                Continue For
            End If

            Dim output As String = Path.Combine(dirPath, layer.DisplayName & ".png")
            layer.Save(output, New PngOptions() With {
                .ColorType = PngColorType.TruecolorWithAlpha
            })
        Next
    End Sub
    ' How to save each layer as a separate PNG file.

    ' How to get the count of layers and know the layers name.
End Class

Thanks.
That’s ok.