本網站上的其他帖子是Simple VB.Net Wrapper for Ghostscript Dll。它也不適用於我。
首先,源代碼進行到InOutErrCallBack方法在這一部分:
gsapi_init_with_args(InstanceHndl, NumArgs + 1, Args)
當此occurr,執行凍結。發生這種情況後,以橫檔方法的下一個調用這部分返回false:
If gsapi_new_instance(InstanceHndl, IntPtr.Zero) <> 0 Then
Return False
Exit Function
End If
gsapi_new_instance的返回值是-100: Ghostscript API - Return codes
的gswin32.dll是在C:\ Windows \ System32下和BIN項目文件夾。
我寫的其他包裝過了,會發生類似的事情:
Option Explicit On
Imports System.Runtime.InteropServices
'''
''' https://msdn.microsoft.com/en-us/library/aa719104(v=vs.71).aspx
''' http://www.codeproject.com/Articles/32274/How-To-Convert-PDF-to-Image-Using-Ghostscript-API
''' http://www.ghostscript.com/doc/current/API.htm
''' https://stackoverflow.com/questions/16929383/simple-vb-net-wrapper-for-ghostscript-dll
'''
Public Class GhostscriptDLLWrapper
#Region "GhostScript Import"
<StructLayout(LayoutKind.Sequential)> _
Public Structure GSVersion
Public product As IntPtr
Public copyright As IntPtr
Public revision As Integer
Public revisionDate As Integer
End Structure
'''
''' <summary>This function returns the revision numbers and strings of the Ghostscript interpreter library;
''' you should call it before any other interpreter library functions to make sure that the
''' correct version of the Ghostscript interpreter has been loaded.</summary>
'''
<DllImport("gsdll32.dll", CharSet:=CharSet.Ansi, EntryPoint:="gsapi_revision")> _
Public Shared Function GsapiRevision(ByRef pVer As GSVersion, ByVal pSize As Integer) As Integer
End Function
''' <summary>Create a new instance of Ghostscript.</summary>
''' <param name="pinstance"></param>
''' <param name="caller_handle"></param>
''' <returns>The instance passed to other GS function</returns>
<DllImport("gsdll32.dll", CharSet:=CharSet.Ansi, EntryPoint:="gsapi_new_instance")> _
Public Shared Function GsapiNewInstance(_
ByRef instance As IntPtr, _
ByVal caller_handle As IntPtr) As Integer
End Function
''' <summary>This will make the conversion</summary>
''' <param name="instance"></param><param name="argc"></param>
''' <param name="argv"></param>
''' <returns>0 if is ok</returns>
<DllImport("gsdll32.dll", CharSet:=CharSet.Ansi, EntryPoint:="gsapi_init_with_args")> _
Public Shared Function GsapiInitWithArgs(_
ByVal instance As IntPtr, _
ByVal argc As Integer, _
<MarshalAs(UnmanagedType.LPArray, ArraySubType:=UnmanagedType.LPStr)> _
ByVal argv() As String) As Integer
End Function
''' <summary>Exit the interpreter</summary>
''' <param name="instance"></param>
''' <returns></returns>
<DllImport("gsdll32.dll", CharSet:=CharSet.Ansi, EntryPoint:="gsapi_exit")> _
Public Shared Function GsapiExit(ByVal instance As IntPtr) As Integer
End Function
''' <summary>Destroy an instance of Ghostscript.</summary>
''' <param name="instance"></param>
<DllImport("gsdll32.dll", CharSet:=CharSet.Ansi, EntryPoint:="gsapi_delete_instance")> _
Public Shared Function GsapiDeleteInstance(ByVal instance As IntPtr)
End Function
#End Region
End Class
我把這樣這些方法:
'https://stackoverflow.com/questions/16929383/simple-vb-net-wrapper-for-ghostscript-dll
'Shift arguments to begin at index 1 (Ghostscript requirement)
'Initialise the interpreter. This calls gs_main_init_with_args() in imainarg.c. See below for return codes.
'The arguments are the same as the "C" main function: argv[0] is ignored and the user supplied arguments are argv[1] to argv[argc-1].
Dim argsCount As Integer = args.Length
ReDim Preserve args(argsCount)
System.Array.Copy(args, 0, args, 1, argsCount)
SyncLock SyncRoot
Dim gsInstance As IntPtr
Dim result As Integer
result = GhostscriptDLLWrapper.GsapiNewInstance(gsInstance, IntPtr.Zero)
If (result = 0) Then
Try
result = GhostscriptDLLWrapper.GsapiInitWithArgs(_
gsInstance, _
args.Length, _
args _
)
If (result < 0) Then
Throw New InvalidOperationException("Erro ao converter páginas do PDF em imagens PNG")
End If
If File.Exists(outputImgPath) Then
objFileStream = New FileStream(outputImgPath, FileMode.Open)
Dim length As Int32 = objFileStream.Length
Dim bytes(length) As Byte
objFileStream.Read(bytes, 0, length)
objFileStream.Close()
objMemoryStream = New MemoryStream(bytes, False)
objImages.Add(Image.FromStream(objMemoryStream))
Else
Throw New InvalidOperationException("Erro ao converter páginas do PDF em imagens PNG")
End If
Catch ex As Exception
Throw New InvalidOperationException("Erro ao converter páginas do PDF em imagens PNG", ex)
End Try
End If
GhostscriptDLLWrapper.GsapiExit(gsInstance)
GhostscriptDLLWrapper.GsapiDeleteInstance(gsInstance)
End SyncLock
我的參數序列:
Dim outputImgPath As String
outputImgPath = "C:\Download\DocumentosV2\Protocolo\Pronunciamento\" + Guid.NewGuid.ToString("N") + ".png"
Dim args() As String = { _
"-dNOPAUSE", _
"-dBATCH", _
"-dSAFER", _
"-dQUIET", _
"-sDEVICE=png16m", _
String.Format("-r{0}", resolucao), _
"-dTextAlphaBits=2", _
"-dGraphicsAlphaBits=2", _
String.Format("-dFirstPage={0}", pageNumber), _
String.Format("-dLastPage={0}", pageNumber), _
String.Format("-sOutputFile={0}", outputImgPath), _
"-f", _
pdfPath _
}
非常感謝。那就是訣竅。我只需要在函數名中使用GhostscriptDllLib.GS。非常感謝!!!! – user3162828 2014-10-21 12:16:42