0

我希望你们都很好,今天我遇到了一个非常令人困惑的问题。我正在尝试制作一个简单的应用程序,可以将 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");

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

在此处输入图像描述

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

最好的,马赫

4

2 回答 2

2

此图像的问题在于它不包含“合并图像”。这是结合了 PSD 文件中所有图层的图像。读者现在自己创建了这个合并的图像。

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

于 2016-02-28T21:14:36.117 回答
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;

        }

    }
}
于 2018-05-16T16:59:01.700 回答