Since I didn't found a good solution on .NET to get image file info (such as width, height, etc.) without loading image file (or stream) into System.Drawing.Bitmap object, which may consume a lot of system resources and even crash an ASP.NET application, I'm trying to load GFL SDK gflGetFileInformation using <DllImport ...> attribute, provided by System.Runtim.InteropServices.
In such way, I could get a results from some GFL SDK methods, but not from gflGetFileInformation - .NET returns an error: Object reference not set to an instance of an object.
May be I did something wrong, but I feel that there must be something with the GFL_FILE_INFORMATION which I cannot figure out.
Do somebody may help me to resolve this problem?
VB.NET Source code used:
'''''''''''''''''''''''''''
Code: Select all
Imports System.Runtime.InteropServices
Public Class ImageFile
<DllImport("libgfl201.dll", _
EntryPoint:="gflLibraryInit", _
SetLastError:=True, _
CharSet:=CharSet.Ansi, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function LibraryInit() As Integer
End Function 'this one runs fine
<DllImport("libgfl201.dll", _
EntryPoint:="gflLibraryExit", _
SetLastError:=True, _
CharSet:=CharSet.Ansi, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Sub LibraryExit()
End Sub 'this one runs fine
<DllImport("libgfl201.dll", _
EntryPoint:="gflGetFileInformation", _
SetLastError:=True, _
CharSet:=CharSet.Ansi, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function GetImageFileInfo(ByVal filename As String, ByVal index As Integer, ByRef information As GFL_FILE_INFORMATION) As Integer
End Function 'this one returns ObjectNullException
<DllImport("libgfl201.dll", _
EntryPoint:="gflGetErrorString", _
SetLastError:=True, _
CharSet:=CharSet.Ansi, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function GetErrorString(ByVal ImageFileError As Integer) As String
End Function 'runs fine, i could see a correct return result
<DllImport("libgfl201.dll", _
EntryPoint:="gflGetFormatIndexByName", _
SetLastError:=True, _
CharSet:=CharSet.Ansi, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function GetFormatIndexByName(ByVal FormatName As String) As Integer
End Function 'this one runs fine, I could see a correct result
Public Declare Ansi Function gflGetFileInformation Lib "libgfl201.dll" (ByVal filename As String, ByVal Index As Long, ByRef info As GFL_FILE_INFORMATION) As Integer ' this one also returns an ObjectNullException 'used to test another version of DLL method declaration - same ObjectNullException
'This structure was reconstructed from "include/libgfl.h" as the source:
Public Structure GFL_FILE_INFORMATION
Public Type As System.UInt16
Public Origin As System.UInt16
Public Width As System.Int32
Public Height As System.Int32
Public FormatIndex As System.Int32
Public FormatName As System.String
Public Description As System.String
Public Xdpi As System.UInt16
Public Ydpi As System.UInt16
Public BitsPerComponent As System.UInt16
Public ComponentsPerPixel As System.UInt16
Public NumberOfImages As System.Int32
Public FileSize As System.UInt32
Public ColorModel As System.Int32
Public Compression As System.Int32
Public CompressionDescription As System.String
Public XOffset As System.Int32
Public YOffset As System.Int32
End Structure
End Class
Class ImageFileInfoTester
Public Sub Main()
Dim filename = "D:\somefile.jpg" 'permissions even set to everyone for test purposes
Dim fileinfo As New ImageFile.GFL_FILE_INFORMATION
'Dim fileinfo As ImageFile.GFL_FILE_INFORMATION 'makes no difference
Dim result As Integer
Images.ImageFile.LibraryInit()
result = Images.ImageFile.GetImageFileInfo(filename, -1, fileinfo)
Images.ImageFile.LibraryExit()
End Sub
End Class
Thank you,
ViTAR