I'm trying to instantiate a new MagickImageCollection using a byte array, which should be an option from looking at the overloaded constructors. My goal is to convert a multi-page PDF into a collection of MagickImage objects, but I get the following error:
FailedToExecuteCommand `\"gswin32c.exe\" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 \"-sDEVICE=pngalpha\" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 \"-r300x300\" \"-sOutputFile=C:/Users/KYLERJ~1.CIG/AppData/Local/Temp/magick-20604AaUhIVsEcWg%d\" \"-fC:/Users/KYLERJ~1.CIG/AppData/Local/Temp/magick-2060qx27_qu8fxBC\" \"-fC:/Users/KYLERJ~1.CIG/AppData/Local/Temp/magick-2060bh7xT1NnEmmm\"' (The system cannot find the file specified.\r\n) @ error/delegate.c/ExternalDelegateCommand/475
Here's my code:
public void ConvertPDFToImages(byte[] pdfBytes, string saveImgToPath, string saveThumbnailToPath = null)
{
MagickReadSettings readSettings = new MagickReadSettings();
readSettings.Density = new Density(300, 300); // make this configurable
readSettings.Format = MagickFormat.Pdf;
using (MagickImageCollection images = new MagickImageCollection(pdfBytes, readSettings))
{
//images.Read(pdfBytes, settings);
for (int i = 0; i > images.Count; i++)
{
int docNum = i + 1;
string fullImgPath = FormatPath(saveImgToPath, docNum);
string fullThumbPath = FormatPath(saveThumbnailToPath, docNum);
_imageResizingService.OptimizeImage(images[0].ToByteArray(), fullImgPath, fullThumbPath);
}
}
}
I have tried passing the pdfBytes along with the readSettings into the constructor in the using statement as you see above as well as passing them into the MagickImageCollection.Read() method just inside of the using statement. I get the same error I included above.
Convert.FromBase64String() methodMagickReadSettings.Format = MagickFormat.Pdf;OS: Windows 10 Enterprise
OS Version: 10.0.17134 Build 17134
Framework: C# .Net Core 2.1.3
Did you install Ghostscript on your server/machine?
@dlemstra No, I just installed the Nuget package. I must have overlooked that part of the docs. I've been using it to convert and optimize images by passing in the images as byte[] to the MagickImage constructor without any issues, though. Doesn't it use similar commands under the hood?
A PDF file needs to have Ghostscript installed and you probably need to pay for it when you use it commercially. For most other image formats you don't need to install something.
@dlemstra That solved the problem! My apologies and I appreciate the help!
Most helpful comment
A PDF file needs to have Ghostscript installed and you probably need to pay for it when you use it commercially. For most other image formats you don't need to install something.