24-12-16, 03:13 AM
(آخر تعديل لهذه المشاركة : 24-12-16, 03:39 AM {2} بواسطة silverlight.)
الكلاسات بعد التعديل عليها قليلا
الكلاسات مكتوبة باستخدام الفيحوال استوديو 2015
استخدامهم مع نسخ قديمة قد يؤدي الي خطأ في الكود
الإستخدام
يُنصح باستخدام الكلاسات مع بيانات موجودة فعليا علي الكمبيوتر
تمرير بيانات خاطئة قد يؤدي الي Exceptions و حتي الأن هذا لم يتم مراعاته في الكود المكتوب
يُنصح بالاستغناء عن هذا الجزء من الكود في الكلاس CairoDataFile
لقد تم كتابته في الكلاس للتوضيح فقط لا غير
الكلاسات مكتوبة باستخدام الفيحوال استوديو 2015
استخدامهم مع نسخ قديمة قد يؤدي الي خطأ في الكود
PHP كود :
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
Public ReadOnly Property ToStream As IO.MemoryStream
Get
Return New IO.MemoryStream(ToBytes())
End Get
End Property
End Class
PHP كود :
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
الإستخدام
يُنصح باستخدام الكلاسات مع بيانات موجودة فعليا علي الكمبيوتر
تمرير بيانات خاطئة قد يؤدي الي Exceptions و حتي الأن هذا لم يتم مراعاته في الكود المكتوب
PHP كود :
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
يُنصح بالاستغناء عن هذا الجزء من الكود في الكلاس CairoDataFile
لقد تم كتابته في الكلاس للتوضيح فقط لا غير
PHP كود :
Public ReadOnly Property ToStream As IO.MemoryStream
Get
Return New IO.MemoryStream(ToBytes())
End Get
End Property

