MagickImage - 将PSD转换为PNG - 无法在生成的PNG中渲染图层样式

时间:2016-02-28 19:46:21

标签: c# imagemagick png psd magick.net

我希望你们都很好,今天我遇到了一个相当令人困惑的问题。 我正在尝试制作一个可以将PSD转换为透明PNG的简单应用程序。但我对我得到的结果不满意。

我在C#中使用Magick.NET-Q16-x86.DLL(MagickImage)

以下是我的代码段,请查看:

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

以下是解释问题的图片: (左侧是预期结果,右侧图像是我得到的)

enter image description here

所以我不明白这里发生了什么。如果我能得到任何答案,我真的很感激。非常感谢您的评论!

最佳, 马赫

2 个答案:

答案 0 :(得分:2)

此图片的问题在于它不包含合并的图片'。这是组合PSD文件中所有图层的图像。读者现在可以创建这个合并的图像。

问题在于ImageMagick / Magick.NET不支持Photoshop的所有功能,这就是它创建此图像的原因。未来可能会读取图像,但实现所有PSD功能需要花费大量时间。

答案 1 :(得分:1)

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;

        }

    }
}