File To IntPtr
The Purpose of the below class is to convert a String to IntPtr
Public Class CairoIntPtr
Inherits MarshalByRefObject
Implements IDisposable
Private _disposed As Boolean
Private _handle As IntPtr
Public Sub New(s As String)
Dim func As Func(Of String, IntPtr) = Function(str)
Dim ptr As IntPtr = CType(Nothing, IntPtr)
ptr = System.Runtime.InteropServices.Marshal.StringToHGlobalUni(str)
Return ptr
End Function
Dim zero As Func(Of IntPtr) = Function() IntPtr.Zero
_handle = If(Not String.IsNullOrEmpty(s), func(s), zero)
End Sub
Public ReadOnly Property Handle() As Integer
Get
Return Me._handle
End Get
End Property
Public ReadOnly Property ToInt32 As Int32
Get
Return Convert.ToInt32(Me.Handle)
End Get
End Property
Public ReadOnly Property ToInt64 As Int64
Get
Return Convert.ToInt64(Me.Handle)
End Get
End Property
Private Sub DisposeInternal()
If Me._handle <> 0 Then
Dim action As Action(Of IntPtr) = Sub(ptr As IntPtr)
System.Runtime.InteropServices.Marshal.FreeHGlobal(ptr)
End Sub
action(_handle)
Me._handle = 0
End If
End Sub
Protected Overridable Sub Dispose(disposing As Boolean)
If _disposed Then
Return
End If
If disposing Then
Me.DisposeInternal()
End If
_disposed = True
End Sub
Public Overrides Function Equals(obj As Object) As Boolean
Return obj IsNot Nothing AndAlso obj.[GetType]() Is MyBase.[GetType]() AndAlso (CType(obj, CairoIntPtr))._handle = Me.Handle
End Function
Public Overrides Function GetHashCode() As Integer
Return Me._handle.GetHashCode()
End Function
Protected Overrides Sub Finalize()
Dispose(False)
MyBase.Finalize()
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
End Class
Inherits MarshalByRefObject
Implements IDisposable
Private _disposed As Boolean
Private _handle As IntPtr
Public Sub New(s As String)
Dim func As Func(Of String, IntPtr) = Function(str)
Dim ptr As IntPtr = CType(Nothing, IntPtr)
ptr = System.Runtime.InteropServices.Marshal.StringToHGlobalUni(str)
Return ptr
End Function
Dim zero As Func(Of IntPtr) = Function() IntPtr.Zero
_handle = If(Not String.IsNullOrEmpty(s), func(s), zero)
End Sub
Public ReadOnly Property Handle() As Integer
Get
Return Me._handle
End Get
End Property
Public ReadOnly Property ToInt32 As Int32
Get
Return Convert.ToInt32(Me.Handle)
End Get
End Property
Public ReadOnly Property ToInt64 As Int64
Get
Return Convert.ToInt64(Me.Handle)
End Get
End Property
Private Sub DisposeInternal()
If Me._handle <> 0 Then
Dim action As Action(Of IntPtr) = Sub(ptr As IntPtr)
System.Runtime.InteropServices.Marshal.FreeHGlobal(ptr)
End Sub
action(_handle)
Me._handle = 0
End If
End Sub
Protected Overridable Sub Dispose(disposing As Boolean)
If _disposed Then
Return
End If
If disposing Then
Me.DisposeInternal()
End If
_disposed = True
End Sub
Public Overrides Function Equals(obj As Object) As Boolean
Return obj IsNot Nothing AndAlso obj.[GetType]() Is MyBase.[GetType]() AndAlso (CType(obj, CairoIntPtr))._handle = Me.Handle
End Function
Public Overrides Function GetHashCode() As Integer
Return Me._handle.GetHashCode()
End Function
Protected Overrides Sub Finalize()
Dispose(False)
MyBase.Finalize()
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
End Class
The purpose of the below class is to convert IntPtr to File
Public Class CairoDataFile
Private _data As Byte()
Private _cairo As String
Public Sub New(ptr As IntPtr)
Dim func As Func(Of IntPtr, String) = Function(p) System.Runtime.InteropServices.Marshal.PtrToStringUni(p)
_cairo = If(Nothing, func(ptr))
Me._data = (If(Not String.IsNullOrEmpty(_cairo), (New Lazy(Of Byte())(Function() IO.File.ReadAllBytes(_cairo))).Value, (New Lazy(Of Byte())(Function() New Byte() {})).Value))
End Sub
Public ReadOnly Property ToText As String
Get
Return _cairo
End Get
End Property
Public ReadOnly Property ToBytes As Byte()
Get
Return Me._data
End Get
End Property
' It is recommended to remove the below property, it is written for demonstration purpose
Public ReadOnly Property ToStream As IO.MemoryStream
Get
Return New IO.MemoryStream(ToBytes())
End Get
End Property
End Class
Private _data As Byte()
Private _cairo As String
Public Sub New(ptr As IntPtr)
Dim func As Func(Of IntPtr, String) = Function(p) System.Runtime.InteropServices.Marshal.PtrToStringUni(p)
_cairo = If(Nothing, func(ptr))
Me._data = (If(Not String.IsNullOrEmpty(_cairo), (New Lazy(Of Byte())(Function() IO.File.ReadAllBytes(_cairo))).Value, (New Lazy(Of Byte())(Function() New Byte() {})).Value))
End Sub
Public ReadOnly Property ToText As String
Get
Return _cairo
End Get
End Property
Public ReadOnly Property ToBytes As Byte()
Get
Return Me._data
End Get
End Property
' It is recommended to remove the below property, it is written for demonstration purpose
Public ReadOnly Property ToStream As IO.MemoryStream
Get
Return New IO.MemoryStream(ToBytes())
End Get
End Property
End Class
Usage:
Both classes may used together to read any file in computer
Example: Read an image from application directory and use it as background for the form
Dim filePath As String = ".\tmp-0.gif"
Dim ptr As IntPtr = New CairoIntPtr(filePath).Handle
Dim cdf As New CairoDataFile(ptr)
If IO.File.Exists(cdf.ToText) Then
Me.BackgroundImageLayout = ImageLayout.Center
Me.BackgroundImage = Image.FromStream(cdf.ToStream)
End If
Dim ptr As IntPtr = New CairoIntPtr(filePath).Handle
Dim cdf As New CairoDataFile(ptr)
If IO.File.Exists(cdf.ToText) Then
Me.BackgroundImageLayout = ImageLayout.Center
Me.BackgroundImage = Image.FromStream(cdf.ToStream)
End If
Comments
Post a Comment