Thanks for the byte array trick, definitely saves some operations and wasted memory!
I'm still having trouble though, I've tried modifying my code to use either of these two blocks and neither generate the correct dpi or maintain transparency for that image. I also tried the Height/Width settings in the MagickReadSettings instead of image.Resize(x,y) but the image maintained the original size. This issues seems to be specific to all ghostscript files, I'm having the same issue with transparency in .ai, .psd and eps files.
Right now I'm trying to convert that eps to a png with 72 dpi (currently 300dpi) and a max resolution of 400x400 while maintaining aspect ratio.
I'm still having trouble though, I've tried modifying my code to use either of these two blocks and neither generate the correct dpi or maintain transparency for that image. I also tried the Height/Width settings in the MagickReadSettings instead of image.Resize(x,y) but the image maintained the original size. This issues seems to be specific to all ghostscript files, I'm having the same issue with transparency in .ai, .psd and eps files.
Right now I'm trying to convert that eps to a png with 72 dpi (currently 300dpi) and a max resolution of 400x400 while maintaining aspect ratio.
public void ResizeImage(IFileInfo sourceFile, IEnumerable<ImageSize> sizes) {
if (this.IsValidFormat(sourceFile)) {
var resizes = sizes as ImageSize[] ?? sizes.ToArray();
if (sizes == null || !resizes.Any()) {
return;
}
sizes = resizes.Max(s => s.MaxHeight) > resizes.Max(s => s.MaxWidth) ? resizes.OrderByDescending(s => s.MaxHeight) : resizes.OrderByDescending(s => s.MaxWidth);
foreach (var size in sizes) {
if (!size.MatteImage) {
size.CalculateSize(this.GetImageInformation(sourceFile)[0]);
}
var settings = new MagickReadSettings {
ColorSpace = global::ImageMagick.ColorSpace.RGB,
Density = new MagickGeometry(size.Dpi, size.Dpi)
};
using (var image = new MagickImage(sourceFile.Get())) {
image.Format = this.GetImageFormat(size.Destination);
image.Resize(size.MaxWidth, size.MaxHeight);
size.Destination.Save(image.ToByteArray());
}
}
}
}
and public void ResizeImage(IFileInfo sourceFile, IEnumerable<ImageSize> sizes) {
if (this.IsValidFormat(sourceFile)) {
var resizes = sizes as ImageSize[] ?? sizes.ToArray();
if (sizes == null || !resizes.Any()) {
return;
}
sizes = resizes.Max(s => s.MaxHeight) > resizes.Max(s => s.MaxWidth) ? resizes.OrderByDescending(s => s.MaxHeight) : resizes.OrderByDescending(s => s.MaxWidth);
foreach (var size in sizes) {
if (!size.MatteImage) {
size.CalculateSize(this.GetImageInformation(sourceFile)[0]);
}
var settings = new MagickReadSettings {
ColorSpace = global::ImageMagick.ColorSpace.RGB,
Density = new MagickGeometry(size.Dpi, size.Dpi)
};
using (var image = new MagickImage(sourceFile.Get(), settings)) {
image.Format = this.GetImageFormat(size.Destination);
image.Resize(size.MaxWidth, size.MaxHeight);
size.Destination.Save(image.ToByteArray());
}
}
}
}