简体   繁体   中英

Unable to convert PDF to any Image format in C# With Imagemagick

this is my first question here so please don't be to harsh on me :) Anyway, let's get right to it:

For an application I need to convert a PDF file to an Image file (Specific format doesn't matter but preferably png or jpg). To get this done, I try to use ImageMagick but when I try to convert anything, it will throw an error. Now after some research I came to the conclusion I needed to have Ghostscript installed, which I tried to get from the NuGet package manager integrated in Visual Studio 2017. Anyway, when I try to install said package, it throws the following error:

Severity Code Description Project File Line Suppression >State Error Failed to add reference to 'gsdll32'. Please make sure that the file is accessible, and that it is a valid >assembly or COM component.

I'm trying to accomplish this using Visual Studio 2017 with C#. The API's I am using are: +Magick.NET-Q16-AnyCPU V7.11.1 +GhostScriptSharp V1.3.1.4 +Ghostscript V9.2.0 (Throws error)

In case it is required to understand what I am trying, here is the code that I am trying to compile:

using ImageMagick.Configuration;
using ImageMagick.Defines;
using ImageMagick.ImageOptimizers;
using ImageMagick;

using GhostscriptSharp;
using GhostscriptSharp.Settings;
        public MagickImageCollection PDFOutput;
        public Image Current;
        public org.pdfclown.documents.Page CurrentPage;

        private void BtnConvert_Click(object sender, EventArgs e)
        {
            if(openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    ImageMagick.MagickNET.Initialize();
                    MagickReadSettings Settings = new MagickReadSettings();
                    Settings.Density = new Density(300, 300);
                    Settings.Format = MagickFormat.Jpg;

                    using (MagickImageCollection Images = new MagickImageCollection())
                    {
                        Images.Add(openFileDialog1.FileName);
                        int Page = 1;
                        int i = 0;
                        foreach(MagickImage Image in Images)
                        {
                            Image.Write("FilePage #" + Page);
                            PDFOutput[i] = Image;
                            Page++;
                            i++;
                        }
                        MessageBox.Show(PDFOutput.Count.ToString());
                    }
                }
                catch(Exception E)
                {
                    MessageBox.Show(E.Message);
                }

Am I missing something regarding the GhostScipt install? Does it only work when downloaded directly from the GhostScript website?

I hope that I have provided enough context to my problem and I'll be looking forward to any answers I may get on this.

Many thanks in advance!!

Kind Regards, Melvin

Yes GhostScript is licensed in such a way, that people don't include it in their wrappers/nugets. You need to make sure you have the dll.

You generally have to download it ( gsdll32.dll ), add it to the project and output it to your output path (or anything similar, like install it) so that your application can find the gsdll32.dll and load it.

Also note you will need the appropriate bitness aswell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM