2012-09-05 67 views
3

我正嘗試使用GhostScript從pdf創建圖像。這裏是我的代碼:使用GhostScript將PDF轉換爲圖像 - 我如何引用gsdll32.dll?

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

我的問題是,當我在我的網頁上運行我的代碼,我得到這個錯誤:

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

我有實際的dll文件,我想也許我只需要添加一個引用到我的bin文件夾,但是當我嘗試時,我得到這個錯誤:

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

所以我有點卡 - 我有DLL,但我不知道如何引用它。任何人都知道我需要做什麼?

+0

解決方案是什麼? 昨天對我來說這工作得很好。然後我移動了整個網站的目錄,現在什麼也沒有。我可以使用包管理器控制檯,但是當我部署到服務器時,我需要它在那裏工作。 – Mike

回答

2

據我所知,你不能只是「添加引用」的DLL,除非可能是DLL是爲C#編寫或.NET,這Ghostscript的不是,它在C

你寫需要使用Win32 API調用'LoadLibrary'或任何C#/。NET等價物。

您的第一個錯誤看起來像是無法找到DLL,您是否在啓動應用程序時獲得了當前目錄中DLL的副本?

4

在包管理器控制檯類型:安裝,包裝Ghostscript.Net

0

嘗試用DLL的完整路徑而不是僅僅名稱。就像如果你的DLL保持在

D:\TestApplication\bin\gsdll32.dll

然後,

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

上面的語句將

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

相關問題