Background Image layer and Alpha Channel creation for PSD (C#)

how to add new layer upno the psd image

@veerakarthik,

Can you please try to use following sample code on your end and share feedback with us if there is still an issue.

string outputFilePath = “PsdResult.psd”;

var filesList = new string[]
{
    "PsdExample.psd",
    "BmpExample.bmp",
    "GifExample.gif",
    "Jpeg2000Example.jpf",
    "JpegExample.jpg",
    "PngExample.png",
    "TiffExample.tif",
};

using (var image = new PsdImage(200, 200))
{
    foreach (var fileName in filesList)
    {
        string filePath = fileName;
        using (var stream = new FileStream(filePath, FileMode.Open))
        {
            Layer layer = null;
            try
            {
                 layer = new Layer(stream);
                 image.AddLayer(layer);
            }
            catch (Exception e)
            {
                if (layer != null)
                {
                    layer.Dispose();
                }

                throw e;
            }
        }
    }

    image.Save(outputFilePath);
}

Thanks sir,
Then, this code used psd new layer didn’t draw…

png image draw the psd layer viwe issue for image is strech.

How to one psd layer to add on another psd layer?

Then one psd layer size is 1920 * 1080
another psd layer size is 2400 * 3000

This two psd layer save an single psd image it’s not saved.

@veerakarthik,

I have observed your scenario and request you to please share the working example along with sample code reproducing the issue on your end. Moreover, can you please share the elaboration for following comments too.

Hi,
How to add alpha chanels within psd layer

@veerakarthik,

I have observed your requirements and regret to share that at present the requested support is not available in API. An issue with ID PSDNET-226 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 feature will be available.

ok sir…
Any other updates in alpha channel create on particular layer in PSD image

@veerakarthik,

As I have shared earlier with you that this support is not available in API. The tentative ETA for this support is by end of Q1 of 2020. In mean while, I suggest you to please consider using following code if that helps as a workaround. In the following case we save transparency of transparent PNG file.

            string layer1File = "Layer1.psd";
            string layer2File = "Layer2.png";
            string layer3File = "Layer3.png";

            Layer Layer2 = null;
            Layer Layer3 = null;
            using (PsdImage image = (PsdImage)Image.Load(layer1File))
            {
                using (var stream = new FileStream(layer2File, FileMode.Open))
                {
                    Layer2 = new Layer(stream);

                    Layer2.Resize(1200, 1200);
                    var width = Layer2.Width;
                    var height = Layer2.Height;

                    Layer2.Left = 675;
                    Layer2.Top = 0;

                    Layer2.Right = Layer2.Left + width;
                    Layer2.Bottom = Layer2.Top + height;

                    image.AddLayer(Layer2);
                }

                using (var stream = new FileStream(layer3File, FileMode.Open))
                {
                    Layer3 = new Layer(stream);
                    Layer3.Resize(550, 550);

                    var width = Layer3.Width;
                    var height = Layer3.Height;

                    Layer3.Left = 1200;
                    Layer3.Top = 300;

                    Layer3.Right = Layer3.Left + width;
                    Layer3.Bottom = Layer3.Top + height;

                    image.AddLayer(Layer3);
                }

                string outFileName = "finaloutput.psd";
                image.Save(outFileName, new PsdOptions());
            }

Thanks,
This code is working fine

how to set background layer in particular layers

@veerakarthik,

Can you please elaborate your requirements as I have not been able to completely understand that. Please share the sample PSD with desired effect that you want to generate using Aspose.PSD.

HI Sir,
one psd layer upon the another image draw.
Then, any one particular layer to assign the background layer is possible or not.

@veerakarthik,

I like to inform that PSD stores background layer in various ways. The most often case, if psd has only one background layer, then PSD doesn’t contain layer information and it has only preview. But if we have more than one layer then background layer is a layer in layers’ list with locked opacity, position and etc.

how to assign background layer?
Then assign background layer after didn’t move any other position and etc…

Background from Layer :

this side refer but aspose using psd layer draw.
but output is not correct

@veerakarthik,

I have observed your comments and created investigation ticket with ID PSDNET-313 to further investigate either your requirements are possible with Aspose.PSD or not. We will share good news with you soon.

aspose.imageing useing the backgroundlayer create

@veerakarthik ,

We have made the code snippet that demonstrates how we can detect and create background layer using Aspose.PSD. Can you please verify this on your end and share if it help you. We will also consider adding API for background layer creation in the next release Aspose.PSD subsequent releases as well.

       // Check if Layer is Background
        static bool IsLayerIsBackground(Layer layer)
        {
            return
                !layer.HasAlpha &&
                layer.LayerLock == (LayerLockType.LockTransparentPixels | LayerLockType.LockPosition) &&
                layer.Name == "Background" &&
                layer.Flags == (LayerFlags.HasUsefulInformation | LayerFlags.TransparencyProtected);
        }

        static void MakeLayerToBeBackground(Layer layer)
        {
            layer.Name = "Background";
            layer.LayerLock = LayerLockType.LockTransparentPixels | LayerLockType.LockPosition;
            layer.Flags = layer.Flags | LayerFlags.TransparencyProtected;

            // Removing of Alpha channel
            var channels = new List<ChannelInformation>();
            if (layer.HasAlpha)
            {
                for (int i = 0; i < layer.ChannelsCount; i++)
                {
                    if (layer.ChannelInformation[i].ChannelID != -1)
                    {
                        channels.Add(layer.ChannelInformation[i]);
                    }
                }
                layer.ChannelInformation = channels.ToArray();
            }
        }

        // Example code
        string sourceFileName = "Background.psd";

        // Background layer is created in PhotoShop
        using (var psd = (PsdImage)Image.Load(sourceFileName))
        {
            var layer = psd.Layers[0];
            if (!IsLayerIsBackground(layer))
            {
                throw new Exception("Layer is not Background");
            }

            // Check if after the save layer stays background
            var outputBackgroundPath = "output.psd";
            psd.Save(outputBackgroundPath, new PsdOptions());
        }

        // In this case input image hasn't background layer
        sourceFileName = "NotBackground.psd";

        using (var psd = (PsdImage)Image.Load(sourceFileName))
        {
            var layer = psd.Layers[0];

            if (IsLayerIsBackground(layer))
            {
                throw new Exception("Layer must not be Background at the start");
            }

            // We can make bakground
            MakeLayerToBeBackground(layer);

            if (!IsLayerIsBackground(layer))
            {
                throw new Exception("Layer is not Background");
            }

            // Check if after the save layer became the background layer
            var outputBackgroundPath = "outputCreatedBackground.psd";
            psd.Save(outputB<a class="attachment" href="/uploads/default/32889">PSDNET313_1.zip</a> (3.4 MB)
 ackgroundPath, new PsdOptions());
        }

Thanks, mudassir.fayyaz,

create background layer using Aspose.PSD.
This code using changes are working fine.

Thanks,
veera

@veerakarthik,

You are very welcome.