如何在 C# 中引用 Ghostscript DLL

use*_*792 3 c# pdf ghostscript

我正在使用 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();
    }
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

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

HAB*_*JAN 5

在你的情况下,你应该这样创建 Ghostscript dll 路径:

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