使用GhostScript将pdf转换为图像 - 如何引用gsdll32.dll?

4
我正在尝试使用GhostScript从PDF中创建图像。下面是我的代码:

GhostscriptWrapper.ConvertToBMP(inputPDFFilePath, outputBMPFilePath);

这是我的 GhostscriptWrapper 类:

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 };
    }
}

我的问题是,当我在我的页面上运行代码时,出现以下错误信息:
无法加载DLL“gsdll32.dll”: 找不到指定的模块。(来自HRESULT的异常: 0x8007007E)
我有实际的dll文件,我认为也许我只需要将其引用添加到我的bin文件夹中,但是当我尝试这样做时,我会得到以下错误信息:
无法添加对“D:\gsdll32.dll”的引用。在组件中找不到类型库
所以我有点困惑——我有该dll文件,但我不知道如何引用它。请问有人知道我需要做什么吗?

昨天这个方法还能用啊,可是我把整个网站的目录移动了一下,现在就不行了。我可以使用包管理器控制台,但是当我部署到服务器上时,我需要让它在那里工作。请问解决方案是什么? - Mike
@Mike 这是一篇关于如何解决这个错误的文章。链接。我相信它会解决你的问题。 - Aamerallous
4个回答

5
在程序包管理控制台中输入:Install-Package Ghostscript.Net

2

尝试使用完整的dll路径而不仅仅是名称。比如,如果你的dll存放在

D:\TestApplication\bin\gsdll32.dll

那么,

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

上述语句将变为

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


2
据我所了解,您不能只是“添加引用”到一个DLL,除非可能DLL是为C#或.NET编写的,而Ghostscript不是,它是用C语言编写的。
您需要使用Win32 API调用“LoadLibrary”或C#/.NET等效项。
您的第一个错误看起来像是找不到DLL,您是否在启动应用程序时将DLL副本放在当前目录中?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接