How to save smart objects as png images

How to save smart objects as png images

@houdajin

To save smart objects as PNG images using Aspose.PSD, you can follow the example provided below. This example demonstrates how to load a PSD file, access a smart object layer, and export its contents as a PNG image.

Here’s a sample code snippet in C#:

using System;
using System.IO;
using Aspose.PSD.FileFormats.Png;
using Aspose.PSD.FileFormats.Psd;
using Aspose.PSD.FileFormats.Psd.Layers;
using Aspose.PSD.ImageOptions;

namespace Aspose.PSD.Examples
{
    class SaveSmartObjectAsPng
    {
        public static void Run()
        {
            string baseFolder = "path_to_your_psd_files"; // Set your base folder path
            string outputFolder = "path_to_output_folder"; // Set your output folder path
            string psdFilePath = Path.Combine(baseFolder, "your_file.psd"); // Specify your PSD file

            using (PsdImage image = (PsdImage)Image.Load(psdFilePath))
            {
                // Assuming the smart object is the first layer
                var smartObjectLayer = (SmartObjectLayer)image.Layers[0];

                // Export the contents of the smart object layer
                string pngOutputPath = Path.Combine(outputFolder, "exported_smart_object.png");
                using (var innerImage = (RasterImage)smartObjectLayer.LoadContents(null))
                {
                    innerImage.Save(pngOutputPath, new PngOptions() { ColorType = PngColorType.TruecolorWithAlpha });
                }
            }

            Console.WriteLine("Smart object saved as PNG successfully.");
        }
    }
}

Explanation:

  1. Load the PSD File: The code begins by loading the PSD file using PsdImage.Load().
  2. Access the Smart Object Layer: It accesses the first layer of the image, which is assumed to be a smart object layer.
  3. Export Contents: The contents of the smart object are loaded and saved as a PNG file using the Save method with PngOptions.

Make sure to replace "path_to_your_psd_files" and "path_to_output_folder" with the actual paths where your PSD files are located and where you want to save the PNG images.

This approach allows you to effectively save smart objects from a PSD file as PNG images. If you have any further questions or need additional examples, feel free to ask!

I am using psd-java

@houdajin please check these two options depending on your tasks:

  1. Export Smart Object to its real format and then convert it to PNG (In this case you’ll get the source of SmartObject):
    Open PSD, PSB and AI files and export them to PDF, PNG, TIFF, GIF, BMP, JPEG|Documentation
string exportContentPath = "somepath";
smartLayer.exportContents(exportContentPath);

  PngOptions pngSaveOpt = new PngOptions();
        pngSaveOpt.setColorType(PngColorType.TruecolorWithAlpha);

        try (PsdImage image = (PsdImage) com.aspose.psd.Image.load(exportContentPath)) {
                image.save(fn, pngSaveOpt );
        }
  1. Use the API of the Layer class (In this case you’ll get the SmartObject rendered pixels):
PngOptions pngSaveOpt = new PngOptions();        pngSaveOpt.setColorType(PngColorType.TruecolorWithAlpha);
smartLayer.save(fn, pngSaveOpt );

Also, according to your activity. Please check Aspose Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team. Dedicated team can help you with the any tasks.