Posts

Showing posts from December, 2016

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.Ha

CopyFromScreen

الكود التالي يوضح كيف يمكن أخذ صورة لشاشة الكمبيوتر بسرعة و حفظها في فهرس المشروع الكود يمكن تنفيذه من خلال تايمر لتصوير الشاشة كل فترة زمنية معينة ثم يتم جمع كل الصور و تحويلها الي ملف صور متحركة أو ملف فيديو ويمكن الاستغناء عن السطر الذي يقوم بحفظ الصورة و نضيف الصور الي مصفوفة وبحيث يسهل التعامل معها كيفما نريد Public Class  Form1     Private Sub Form1_Load ( sender  As  Object ,  e  As  EventArgs )  Handles MyBase . Load         Dim rect  As  Rectangle  =  Screen . PrimaryScreen . Bounds         Using scrBitmap  As  Bitmap  =  Image . FromHbitmap (New  Bitmap ( rect . Width ,  rect . Size . Height ). GetHbitmap (),  Graphics . FromHwnd ( IntPtr . Zero ). GetHdc ())              Using g  As  Graphics  =  Graphics . FromImage ( scrBitmap )                  g . CopyFromScreen ( rect . Location ,  rect . Location ,  rect . Size ,  CopyPixelOperation . SourceCopy )                  scrBitmap . Save (( ".\" & DateTime.Now.ToFileTimeUtc & " . jpg "), Im

Calculate the color of GDI hpalette - GDI+ secrets

    Private Function GetHppaletteColor() As Color         Return Image.FromHbitmap(New Bitmap(Me.Width, Me.Height).GetHbitmap(), IntPtr.Zero).GetPixel(0, 0)     End Function or   Private Function GetHppaletteColor() As Color         Return Image.FromHbitmap(New Bitmap(Me.Width, Me.Height).GetHbitmap(), Graphics.FromHwnd(IntPtr.Zero).GetHdc).GetPixel(0, 0)     End Function the below code shows hoe to save the screen bitmap which hold the hpalette color         Dim rect As Rectangle = CType(Nothing, Rectangle)         Dim devices As Screen() = Screen.AllScreens         For i As Integer = 0 To devices.Length - 1             Dim device As Screen = devices(i)             rect = Rectangle.Union(rect, device.Bounds)         Next         Dim mask As Bitmap = Image.FromHbitmap(New Bitmap(rect.Size.Width, rect.Size.Height).GetHbitmap(), Graphics.FromHwnd(IntPtr.Zero).GetHdc())         Dim fileName As String = Application.StartupPath & "\screen.jpg"         ma

Using Yield to get word count in a string

The below code shows one of the ideas to count the exist of a word in a string   Private Iterator Function GetWordCount(value As String, s As String) As IEnumerable(Of Integer)         Dim index As Integer = -1         Do             index = value.Split(" ").ToList.IndexOf(s, index + 1)             If index >= 0 Then                 Yield index             End If         Loop While index >= 0         Return     End Function Uage:          Dim s As String = "my doc my comp my dog my heart"         Dim result As String = ""         result += String.Format("{0}", GetWordCount(s, "my").Count)

A Function to Convert Generic T To String

    Private Function Convert(Of T)(value As T) As String         Return System.Convert.ToString(CType(value, Object), System.Globalization.CultureInfo.CurrentCulture())     End Function

Control Finder Class

من فترة كنت كتبت بعص الأكواد لإيجاد جميع الكونترول الموجودة في اي كونترول و الكود موجود في اللينك التالي اللينــــــــــــــــــــــــــك لكن و مع التطور الحاصل في الدوت نت قررت أخيرا تحويل معظم الأكواد الي كلاسات بحيث يسهل تطويرها و الاستفادة منها في اي مشروعات مستقبلة و الكود التالي عبارة عن مساهمة جيدة يمكن إضافتها الي اي مكتبة يتم كتابتها للدوت نت Public Class ControlFinder     Private _controls As IEnumerable(Of Control)     Public Sub New(ctrl As Control)         Me.New(ctrl, Function(c) c IsNot Nothing AndAlso Not c.IsDisposed AndAlso c.IsHandleCreated AndAlso c.Visible)     End Sub     Public Sub New(ctrl As Control, criteria As Func(Of Control, Boolean))         If ctrl Is Nothing Then             _controls = New Control() {}         Else             _controls = GetControls(ctrl.Controls, ctrl, criteria)         End If     End Sub     Public ReadOnly Property Controls As IEnumerable(Of Control)         Get             Return _controls