MagickImage - Converting PSD to PNG - Cannot render the Layer Style in Generated PNG

2.1k Views Asked by At

I hope you all are Fine, Today I'm experiencing a quite confusing problem. I'm trying to make a simple application which can convert the PSD to Transparent PNG. But I'm not happy with the results I'm getting.

I'm using Magick.NET-Q16-x86.DLL (MagickImage) with C#

Below is my code snippet, Please review:

ImageMagick.MagickImage image = new MagickImage(filePath+"[0]");
image.Density = new Density("300");
image.Format = MagickFormat.Png32;
image.Write(outputFolder + @"\" + Path.GetFileNameWithoutExtension(filePath) + ".png");

And Here is the Image explaining the Problem: (Left Side is Expected Result and Right Side Image is the one I'm getting )

enter image description here

So I don't understand whats Happening here. I'd really be thankful if i can get any answer. Thanks a lot for reviewing!

Best, Maher

2

There are 2 best solutions below

0
On BEST ANSWER

The issue with this image is that it does not contain a 'merged image'. This is the image that combines all the layers from your PSD file. And the reader now creates this merged image itself.

The problem with this is that ImageMagick/Magick.NET does not support all features of Photoshop and that is why it creates this image. It might be possible that the image can be read in the future but it will take a lot of time to implement all the PSD features.

0
On
Project Console Export Psd to Jpg

public class Tamanho
{
    public string NameFolder { get; set; }
    public int Width { get; set; }
    public int  Heigth { get; set; }
    public string ImagePath { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var PathDefault = @"C:\FOTOSZSA";

        DirectoryInfo di1 = new DirectoryInfo(PathDefault);

        string strPath = @"c:\ImagensImport\LogErro.txt";

        File.Create(strPath).Dispose();

        if (!di1.Exists)
        {

            Directory.CreateDirectory(PathDefault);

        }

        Tamanho settings = new Tamanho { NameFolder = "1000x1000", Width = 1000, Heigth = 1000, ImagePath = @"C:\ImagensImport\imagem1000x1000\" };


        if (Directory.Exists(PathDefault))
        {
            string[] arquivos = Directory.GetFiles(PathDefault);
            var nameFile = "";
            var fileResize = "";
            foreach (string arquivo in arquivos)
            {
                try
                {
                    nameFile = Path.GetFileName(arquivo);

                    if (nameFile.LastIndexOf(".psd") != -1)
                    {
                        using (MagickImage image = new MagickImage(PathDefault + @"\" + nameFile))
                        {

                            ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);

                            Encoder myEncoder = Encoder.Quality;

                            EncoderParameters myEncoderParameters = new EncoderParameters(1);

                            EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder,50L);

                               fileResize = settings.NameFolder;

                                Size newSize = new Size(settings.Width, settings.Heigth);
                                var bmp1 = ResizeImage(image.ToBitmap(), newSize);

                                    myEncoderParameter = new EncoderParameter(myEncoder, 100L);

                                myEncoderParameters.Param[0] = myEncoderParameter;
                                bmp1.Save(settings.ImagePath + nameFile + ".Jpg", jgpEncoder,
                                    myEncoderParameters);
                            image.Dispose();
                            myEncoderParameter.Dispose();
                            myEncoderParameters.Dispose();
                        }

                    }

                }
                catch (Exception ex)
                {

                    using (StreamWriter sw = File.AppendText(strPath))
                    {
                        sw.WriteLine("=============Error File ===========");
                        sw.WriteLine("===========NameFile============= " + nameFile);
                    }
                }

            }
        }


    }

    public static ImageCodecInfo GetEncoder(ImageFormat format)
    {
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
        foreach (ImageCodecInfo codec in codecs)
        {
            if (codec.FormatID == format.Guid)
            {
                return codec;
            }
        }
        return null;
    }

    private static Bitmap ResizeImage(Bitmap mg, Size novoTamanho)
    {
        using (Bitmap i_Bmp = mg)
        {
            double ratio = 0d;
            double myThumbWidth = 0d;
            double myThumbHeight = 0d;
            int x = 0;
            int y = 0;

            Bitmap bp;

            if ((mg.Width / Convert.ToDouble(novoTamanho.Width)) > (mg.Height /
            Convert.ToDouble(novoTamanho.Height)))
                ratio = Convert.ToDouble(mg.Width) / Convert.ToDouble(novoTamanho.Width);
            else
                ratio = Convert.ToDouble(mg.Height) / Convert.ToDouble(novoTamanho.Height);
            myThumbHeight = Math.Ceiling(mg.Height / ratio);
            myThumbWidth = Math.Ceiling(mg.Width / ratio);

            //Size thumbSize = new Size((int)myThumbWidth, (int)myThumbHeight);
            Size thumbSize = new Size((int)novoTamanho.Width, (int)novoTamanho.Height);
            bp = new Bitmap(novoTamanho.Width, novoTamanho.Height);
            x = (novoTamanho.Width - thumbSize.Width) / 2;
            y = (novoTamanho.Height - thumbSize.Height);
            Graphics g = Graphics.FromImage(bp);
            g.FillRectangle(Brushes.White, 0, 0, bp.Width, bp.Height);
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            Rectangle rect = new Rectangle(x, y, thumbSize.Width, thumbSize.Height);
            g.DrawImage(mg, rect, 0, 0, mg.Width, mg.Height, GraphicsUnit.Pixel);

            return bp;

        }

    }
}