【问题标题】:How to reference Ghostscript DLL in C#如何在 C# 中引用 Ghostscript DLL
【发布时间】:2016-12-31 12:42:59
【问题描述】:

我正在使用 C# 包装器使用 Ghostscript 将 PDF 转换为图像,但是我似乎无法正确引用 dll。 我将 DLL 存储在 bin 文件夹中(也不知道这是否是保存它的最佳位置) 这是我的代码:

 byte[] fileData = null;
            using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
            {
                fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength);
            }

    string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);

    Ghostscript.NET.Rasterizer.GhostscriptRasterizer rasterizer = null;
    Ghostscript.NET.GhostscriptVersionInfo vesion = new Ghostscript.NET.GhostscriptVersionInfo(new Version(0, 0, 0), path + @"\gsdll64.dll", string.Empty, Ghostscript.NET.GhostscriptLicense.GPL);
    Stream inStream = new MemoryStream(fileData);
    MemoryStream outStream = new MemoryStream();
    List<Image> imageList = new List<Image>();
    using (rasterizer = new Ghostscript.NET.Rasterizer.GhostscriptRasterizer())
    {
        rasterizer.Open(inStream, vesion, false);
         for (int i = 1; i <= rasterizer.PageCount; i++)
        {
            //string pageFilePath = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(file) + "-p" + i.ToString() + ".jpg");
            int dpi = 200;
            Image img = rasterizer.GetPage(dpi, dpi, i);
            img.Save(outStream, ImageFormat.Jpeg);
            Image img = new Image
            {
                imgByteArray = outStream.ToArray()
            };
            imageList.Add(image);
        }
         rasterizer.Close();
    }

我收到 找不到 Ghostscript 本机库错误。 这是我得到的路径

我认为这与 DLLPath 字符串中的双 / 和 'file://' 有关。我还应该指定 LipPath 吗? 有什么帮助吗??

【问题讨论】:

  • 你的 exe 在 bin\debug 下吗?
  • 不只是 DLL
  • Dll 路径看起来不对,这就是问题所在。你从哪里得到这个“文件:”前缀?它应该看起来像“c:\users\raeda\documents\visual s......\bin\gsdll64.dll”

标签: c# pdf ghostscript


【解决方案1】:

在您的情况下,您应该以这种方式制作 ghostscript dll 路径:

string binPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string gsDllPath = Path.Combine(binPath, Environment.Is64BitProcess ? "gsdll64.dll" : "gsdll32.dll");

【讨论】:

  • 我正在使用您上面的代码,但仍然出现错误。我使用的是 9.26 版,这是最后一个已知的可以与 .NET 包装器互操作的无错误 GS 库。我的错误是:Ghostscript.NET.GhostscriptLibraryNotInstalledException: This managed library is running under 64-bit process and requires 64-bit Ghostscript native library installation on this machine!
  • @bkwdesign 在原生 gs 库中发生了一些变化,我需要调整 Ghostscript.NET 以使用最新的 gs。
  • @HAJAN - 很高兴知道。我的声明last known bug-free GS library 是主观的。我所知道的是,在我将 GS 回滚到这个版本之前,PageCount 和光栅化函数之类的东西并没有像我预期的那样工作。也许我应该进一步回滚?
【解决方案2】:

尽管这不能直接回答问题,但我认为值得一提,因为我在使用 2016 年发布的 Ghostscript.NET 版本 1.2.1 的 OP 代码时遇到了问题。这是我找到的修复:

在源代码中, Ghostscript.NET\Helpers\StreamHelper.cs:171

n = output.Read(buffer, 0, buffer.Length);

真的应该是

n = input.Read(buffer, 0, buffer.Length);

如果您打开的是流而不是路径,而不修改上述代码,您将得到空结果,因为输入流没有被复制。

更深入地读取代码会发现输入流被写入临时文件,然后再次读取到内存中。如果它是磁盘上的 PDF 文件,那么您最好只通过路径而不是流使用 rasterizer.Open

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 2021-05-12
    • 2023-04-08
    • 2013-04-26
    • 1970-01-01
    相关资源
    最近更新 更多