Posts

Showing posts from November, 2016

Image To Color Matrix & Vice-versa

الكلاس التالي يمكن استخدامه في استخلاص ألوان الصورة علي هيئة مصفوفة ثم اعادة كتابتها الي صورة و الكلاس يعطي الإمكانية في التلاعب  بألوان الصورة الكلاس قد يكون بطئ قليلا مع الصور اكبيرة الحجم بسبب حلقة التكرار المستخدمة للحصول علي لون البكسل او إعادة كتابة لون البكسل الي صورة لكن الهدف من الموضوع هو توضيح الفكرة لا أكثر و لا أقل الكود التالي يوضح شكل الكلاس  Public Class Picture     Private pseudoColors As Color(,)     Public Sub New(height As Integer, width As Integer)         pseudoColors = New Color(height - 1, width - 1) {}     End Sub     Public ReadOnly Property Height() As Integer         Get             Return pseudoColors.GetLength(0)         End Get     End Property     Public ReadOnly Property Width() As Integer         Get             Return pseudoColors.GetLength(1)         End Get     End Property     Default Public Property Color(x As Integer, y As Integer) As Color         Get             Return pseudoColors(x, y)         End Get         Set(value As Col

Aggregate String Array

   Public Function AggregateStrings(strings As IEnumerable(Of String), separator As String) As String         Return strings.Aggregate("", Function(txt As String, current As String)                                          If Not String.IsNullOrEmpty(txt) Then                                              Return String.Format("{0}{1}{2}", txt, separator , current)                                          End If                                          Return String.Format("{0}", current)                                      End Function)     End Function Usage:         Dim values As List(Of String) = New List(Of String) From {"100", "200", "300"}         Dim result As String = AggregateStrings(values, " "c)

MeasureString Using Computer Screen

1- Calculate Screen Rectangle     Friend Function ScreenToRectangle() As System.Drawing.Rectangle         Dim rect As System.Drawing.Rectangle = CType(Nothing, System.Drawing.Rectangle)         Dim devices As System.Windows.Forms.Screen() = System.Windows.Forms.Screen.AllScreens         For i As Integer = 0 To devices.Length - 1             Dim device As System.Windows.Forms.Screen = devices(i)             rect = System.Drawing.Rectangle.Union(rect, device.Bounds)         Next         Return rect     End Function 2- Capture Screen Image     Friend Function ScreenToImage() As System.Drawing.Image         Dim screenImage As System.Drawing.Bitmap = New System.Drawing.Bitmap(ScreenToRectangle.Width, ScreenToRectangle.Height)         Using g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(screenImage)             g.CopyFromScreen(ScreenToRectangle.X, ScreenToRectangle.Y, 0, 0, ScreenToRectangle.Size, System.Drawing.CopyPixelOperation.SourceCopy)         End Using       

TextBuilder

Public Structure TextBuilder     Private text As String     Public Sub Add(value As String)         Add(value, "")     End Sub     Public Sub Add(values As IEnumerable(Of String))         For Each value As String In values             If Not value Is Nothing Then                 Me.Add(value)             End If         Next     End Sub     Public Sub Add(value As String, separator As String)         Me.text += value.Aggregate(" "c, Function(s As String, current As String)                                              If Not String.IsNullOrEmpty(s) Then                                                  Return String.Format("{0}{1}{2}", s, separator, current)                                              End If                                              Return String.Format("{0}", current)                                          End Function)     End Sub     Private Function Trim(s As String, separator As Char) As String  

Inventing The Wheel, Simple Practice; Simulate(Of T As IDisposable)

Public Structure Simulate(Of T As IDisposable)     Public Sub New(value As T)         Func = Function() CType(value, IDisposable)     End Sub     Public ReadOnly Property Func As Func(Of IDisposable) End Structure Usage:         Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click         Dim graphicsSimulator As Simulate(Of Graphics) = New Simulate(Of Graphics)(Me.CreateGraphics)         Using CType(graphicsSimulator.Func().Invoke(), Graphics)             CType(graphicsSimulator.Func().Invoke(), Graphics).FillRectangle(SystemBrushes.WindowFrame, New Rectangle(30, 30, 100, 0))         End Using         Dim sqlConnectionSimulator As Simulate(Of System.Data.SqlClient.SqlConnection) = New Simulate(Of System.Data.SqlClient.SqlConnection)(New System.Data.SqlClient.SqlConnection)         Using CType(sqlConnectionSimulator.Func().Invoke(), System.Data.SqlClient.SqlConnection)             Dim simulate = CType(sqlConnectionSimulator.Func().Invoke(), System.Data.

Inventing The Wheel, Simple Practice; Implementing IList (Of T)

Public Class RiverNileList(Of T)     Implements IList(Of T)     Private _list As IList     Public Sub New(list As IList)         _list = list     End Sub     Public ReadOnly Property Count As Integer Implements ICollection(Of T).Count         Get             Return _list.Count         End Get     End Property     Public ReadOnly Property IsReadOnly As Boolean Implements ICollection(Of T).IsReadOnly         Get             Return _list.IsReadOnly         End Get     End Property     Default Public Property Item(index As Integer) As T Implements IList(Of T).Item         Get             Return CType((CObj(_list(index))), T)         End Get         Set(value As T)             _list(index) = value         End Set     End Property     Public Sub Add(item As T) Implements ICollection(Of T).Add         _list.Add(item)     End Sub     Public Sub Clear() Implements ICollection(Of T).Clear         _list.Clear()     End Sub     Public Sub CopyTo(array() As T, arrayIndex As Integer) Implements ICol