Converting a pdf to an image with GhostScript - how do I reference gsdll32.dll?

痴心易碎 提交于 2020-01-02 07:42:09

问题


I'm trying to create an image from a pdf using GhostScript. Here is my code:

GhostscriptWrapper.ConvertToBMP(inputPDFFilePath, outputBMPFilePath);

And here's my GhostscriptWrapper class:

public class GhostscriptWrapper
{
    public static void ConvertToBMP(string inputPath, string outputPath)
    {
        CallAPI(GetArgs(inputPath, outputPath));
    }

    private static void CallAPI(string[] args)
    {
        IntPtr ptr;
        CreateAPIInstance(out ptr, IntPtr.Zero);
        InitAPI(ptr, args.Length, args);
        Cleanup(ptr);
    }

    private static void Cleanup(IntPtr gsInstancePtr)
    {
        ExitAPI(gsInstancePtr);
        DeleteAPIInstance(gsInstancePtr);
    }        

    [DllImport("gsdll32.dll", EntryPoint="gsapi_new_instance")]
    private static extern int CreateAPIInstance(out IntPtr pinstance,
        IntPtr caller_handle);

    [DllImport("gsdll32.dll", EntryPoint="gsapi_delete_instance")]
    private static extern void DeleteAPIInstance(IntPtr instance);

    [DllImport("gsdll32.dll", EntryPoint="gsapi_exit")]
    private static extern int ExitAPI(IntPtr instance);                

    [DllImport("gsdll32.dll", EntryPoint="gsapi_init_with_args")]
    private static extern int InitAPI(IntPtr instance, int argc,
        string[] argv);

    private static string[] GetArgs(string inputPath, string outputPath)
    {
        return new string[] { "-dNOPAUSE", "-dBATCH", "-dSAFER",
            "-dTextAlphaBits=4", "-dGraphicsAlphaBits=4", "-sDEVICE=bmp16m",
             string.Format("-r{0}x{1}", 0x48, 0x48), "-dEPSCrop",
             string.Format("-sOutputFile={0}", outputPath), inputPath };
    }
}

My problem is that when I run my code on my page, I get this error:

Unable to load DLL 'gsdll32.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

I have the actual dll file, I thought maybe I just need to add a reference to my bin folder, but when I try that, I get this error:

A reference to 'D:\gsdll32.dll' could not be added. No type libraries were found in the component

So I'm kind of stuck - I have the dll, but I have no idea how to reference it. Anyone know what I need to do?


回答1:


In Package Manager Console type: Install-Package Ghostscript.Net




回答2:


As I understand it, you can't just 'add a reference' to a DLL, unless possibly the DLL was written for C# or .NET, which Ghostscript is not, its written in C.

You need to use the Win32 API call 'LoadLibrary' or whatever the C#/.NET equivalent is.

Your first error looks like the DLL simply can't be found, have you got a copy of the DLL in the current directory when you start the application ?




回答3:


Check out some other managed Ghostscript wrappers:

  • http://www.wibit.net/blog/integrating_ghostscript_c
  • http://code.google.com/p/gouda/
  • http://www.mattephraim.com/blog/2009/07/08/introducing-ghostscriptsharp/
  • http://www.codeproject.com/Articles/317700/Convert-a-PDF-into-a-series-of-images-using-Csharp

Also check out this other SO article: C# Ghostscript Wrapper




回答4:


Try with complete path of dll instead of only name. Like if your dll kept at

D:\TestApplication\bin\gsdll32.dll

then,

[DllImport("gsdll32.dll", EntryPoint="gsapi_new_instance")]

above statement will be

[DllImport("D:\\TestApplication\\bin\\gsdll32.dll", EntryPoint="gsapi_new_instance")].



来源:https://stackoverflow.com/questions/12287445/converting-a-pdf-to-an-image-with-ghostscript-how-do-i-reference-gsdll32-dll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!