How to add alpha channel in PSD

Hi,
I have added two more layers on the existing PSD file and finally, I could download the output PSD file.
My requirement is, Can I set the Alpha channel on the PSD file?
If is it possible, Can you share the right code for this?

Thanks

@Esakkikumar

I have observed you requirements and have not been able to completely understand them. Can you please share the source PSD file exhibiting your requirements so that we may proceed further to help you.

# How to Save a Selection as an Alpha Channel in Photoshop using .Net?

@Esakkikumar,

I have observed your requirements and an issue with ID PSDNET-226 has been created in our issue tracking system to further investigate the requirements on our end. This thread has been linked with the issue so that you may be notified once the issue will be fixed.

How to save psd file with alpha channel ?
I saw your release notes version Aspose.Imaging for.NET18.7 - IMAGINGNET-2984 The alpha channel enabled by default for PSD images. I am using latest version 19.10 and I couldn’t get the alpha channel on my saved .psd file.
Can you share any sample code for creating or enabling alpha channel?

@Esakkikumar,

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

string sourceFileName =“alphachannel.psd”;
string outputFileName = “result.jpg”;
using (PsdImage image =(PsdImage)
Image.Load(
sourceFileName,
new PsdLoadOptions() { ReadOnlyMode = true, IgnoreAlphaChannel = true }))
{
image.Save(outputFileName, new JpegOptions());
}

We couldn’t get the exact output by using the above code.
Herewith I have attached “Player.psd” file, it has one layer “Layer 1” and Alpha channel also.
When reading this psd file using your code below, the image1 object showing channelscount=5 but inside the Layer object it showing channelscount=4 only.

using (PsdImage image1 = (PsdImage)Aspose.Imaging.Image.Load(“Player.psd”, new PsdLoadOptions() {ReadOnlyMode=true,IgnoreAlphaChannel= true }))
{
image1.Save(“newplayer.psd”,new PsdOptions());
}
After saving the new psd file without changing any properties (newplayer.psd) it doesn’t have alpha channel.
Can you check both the PSD files?
psd files.zip (4.9 MB)

@Esakkikumar,

I like to inform that we have investigated issue on our end. Can you please share desired result with us for further investigation.

I want the resulting psd file should have an alpha channel like same as “player. Psd” file.

@Esakkikumar,

We have tried to understand your requirements and have not been able to completely understand them. Can you please explain that what you actually mean by “set the Alpha channel on the PSD”? 1We haven’t any tools for selection. Do you need to make whole image more transparent or some region of it? Can you please share example of what do you want to get and from what file. We will be able to comprehend your requirements based on shared feedback to help you further.

Hi,
Originally 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.

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

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?

@Esakkikumar,

Thank you for sharing the information with us. With your following comments, I have envisaged that you are trying to set the alpha channel on layer level.

As requested earlier please additionally provide the source file and desired output file that I may link with our issue tracking system for resolution.

Any upadations on this alpha channel creation?

Hi,

Herewith I have attached the source files and desired output p sd file. Original psd file and images and expected output psd.zip (54.9 KB)

Please check and let me know if anything needed.

Thanks

@Esakkikumar,

Thank you for sharing requested files. We have linked this into ticket and will share good news with you soon.

Hi,

Any updates on an issue with ID PSDNET-226?

@Esakkikumar,

I have verified from our issue tracking system and regret to share that at present there are no updates. We request for your patience and will share the good news with you as soon as it will be shared.

Hi,

Any updates on an issue with ID PSDNET-226 ?

@Esakkikumar,

Can you please try to use following sample code on your end and share feedback with us.

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

@Esakkikumar,

We have observed your requirements an in this case we remove background from layer3 and draw it on layer2 with Transparency. Final PSD image has 2 layers, not 3. This is one possible solution too.

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