MagickImage - 将 PSD 转换为 PNG - 无法在生成的 PNG 中呈现图层样式
MagickImage - Converting PSD to PNG - Cannot render the Layer Style in Generated PNG
我希望你们都很好,今天我遇到了一个非常令人困惑的问题。
我正在尝试制作一个可以将 PSD 转换为透明 PNG 的简单应用程序。但我对我得到的结果不满意。
我正在使用 Magick.NET-Q16-x86.DLL (MagickImage) 和 C#
下面是我的代码片段,请查看:
ImageMagick.MagickImage image = new MagickImage(filePath+"[0]");
image.Density = new Density("300");
image.Format = MagickFormat.Png32;
image.Write(outputFolder + @"\" + Path.GetFileNameWithoutExtension(filePath) + ".png");
这是解释问题的图片:
(左侧是预期结果,右侧图像是我得到的图像)
所以我不明白这里发生了什么。如果我能得到任何答案,我真的很感激。非常感谢您的审查!
最好的,
马赫
这张图片的问题在于它不包含 'merged image'。这是结合了 PSD 文件中所有图层的图像。 reader 现在自己创建这个合并图像。
问题是 ImageMagick/Magick.NET 不支持 Photoshop 的所有功能,这就是它创建此图像的原因。以后可能可以读取图片,但是要实现所有的PSD功能需要很多时间。
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;
}
}
}
我希望你们都很好,今天我遇到了一个非常令人困惑的问题。 我正在尝试制作一个可以将 PSD 转换为透明 PNG 的简单应用程序。但我对我得到的结果不满意。
我正在使用 Magick.NET-Q16-x86.DLL (MagickImage) 和 C#
下面是我的代码片段,请查看:
ImageMagick.MagickImage image = new MagickImage(filePath+"[0]");
image.Density = new Density("300");
image.Format = MagickFormat.Png32;
image.Write(outputFolder + @"\" + Path.GetFileNameWithoutExtension(filePath) + ".png");
这是解释问题的图片: (左侧是预期结果,右侧图像是我得到的图像)
所以我不明白这里发生了什么。如果我能得到任何答案,我真的很感激。非常感谢您的审查!
最好的, 马赫
这张图片的问题在于它不包含 'merged image'。这是结合了 PSD 文件中所有图层的图像。 reader 现在自己创建这个合并图像。
问题是 ImageMagick/Magick.NET 不支持 Photoshop 的所有功能,这就是它创建此图像的原因。以后可能可以读取图片,但是要实现所有的PSD功能需要很多时间。
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;
}
}
}