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

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