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

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.

Hi,
Can I set the Alpha channel on the PSD file?

I have added two more layers on the existing PSD file and finally, I could download the output PSD file.

If is it possible, Can you share the right code for this?

This code follow but Alpha channel didn’t created.

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());
        }

@veerakarthik,

This was a workaround approach that we offered for the time being. I also request you to please share the output that you obtained on your end along with source files for which you have observed no Alpha channel. As far as API is concerned, this feature is likely to be available during Q1 of 2020.

Hi,
I have an one psd file (“Layer1.psd”) with 1 layer.
After that I am using the following code to draw the 2 images on the existing psd file.
Finally I will get the new output psd file with 3 layers.

My requirement is I want to set alpha channel for the second layer.
Is it possible to add alpha channel on the second layer while drawing that image on existing psd file?

Layer Layer2= new Layer();
Layer Layer3= new Layer();
using (PsdImage image = (PsdImage)Aspose.Imaging.Image.Load(“Layer1.psd”))
{
image.AddRegularLayer();
image.Layers[1].Name = “Layer 2”;
Layer2 = image.Layers[1]; //** I need alpha channel for this layer alone **//
Layer2.Left = 0;
Layer2.Top = 0;
Layer2.Right = 1920;
Layer2.Bottom = 1080;

                    image.AddRegularLayer();
                    image.Layers[2].Name = "Layer 3";
                    Layer3 = image.Layers[2];
                    Layer3.Left = 0;
                    Layer3.Top = 0;
                    Layer3.Right = 1920;
                    Layer3.Bottom = 1080;                           


                    using (RasterImage drawImage = (RasterImage)Aspose.Imaging.Image.Load("Layer2.png"))
                    {
                        Layer2.DrawImage(new Aspose.Imaging.Point(0, 0), drawImage);   //** I need alpha channel for this layer alone **//                              
                    }

                    using (RasterImage drawImage1 = (RasterImage)Aspose.Imaging.Image.Load("Layer3.png"))
                    {
                        Layer3.DrawImage(new Aspose.Imaging.Point(0, 0), drawImage1);
                    }
                   
                    image.Save("finaloutput.psd", new PsdOptions());
                   
                }

@veerakarthik,

Can you please share the source files and generated output with us.

HI,

   we have opened psd file,  then Layers are 'Background' & 'Layer1'
   Then 'Layer1' is select to channels option to create an Alpha chanel.

This changes are possible or not in aspose.PSD / Aspose.Imaging.

TwoLayers.zip (2.3 MB)

@veerakarthik,

I have associated the information in our issue tracking system and will share the feedback with you as soon as it will be shared by team.

1 Like

HI,

Alpha channels create an particular layer changes.

Any updates on an issue with ID PSDNET-226

@veerakarthik,

We have investigated the requirements further on our end. In the following sample code, we have removed background from layer 3 and draw it on layer 2 with Transparency. The final PSD image has 2 layers, not 3. This is one possible solution.

        private void ReplaceColor(RasterImage image, Color oldColor, int diff, Color newColor)
        {
            var pixels = image.LoadArgb32Pixels(image.Bounds);
            var length = pixels.Length;

            var oldR = oldColor.R;
            var oldG = oldColor.G;
            var oldB = oldColor.B;
            var newColorValue = newColor.ToArgb();

            for (int i = 0; i < length; i++)
            {
                // Red
                var r = (byte)((pixels[i] >> 16) & 0xff);
                // Green
                var g = (byte)((pixels[i] >> 8) & 0xff);
                // Blue
                var b = (byte)(pixels[i] & 0xff);

                int actualDiff = Math.Abs(r - oldR) + Math.Abs(g - oldG) + Math.Abs(b - oldB);

                if (actualDiff <= diff)
                {
                    pixels[i] = newColorValue;
                }
            }

            image.SaveArgb32Pixels(image.Bounds, pixels);
        }

            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);
                    // Replacing White color to Transparent
                    ReplaceColor(Layer3, Color.White, 256, Color.Transparent);
                    Layer2.DrawImage(new Point(0, 0), Layer3);
                }

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