02-10-12, 03:22 PM
وهنا سوف نستخدم نفس الفكرة أو نفس الأسلوب الذي استخدمناه في السؤال السابق وكما تلاحظ عزيزي القارئ أن الأمر يتكرر ولكنه يأخذ أشكالا أخري فقط.
إذن باستخدام جملة For…..Next أيضا نقرأ الألوان الموجودة بصورة ما ثم نضيفها الي مصفوفة ثم نسترجع الألوان من المصفوفة ونستخدمها كيفما شئنا وهنا نحن نسترجع Color
وسوف أعطي مثالا واحدا فقط ويمكنك أنت عزيزي القارئ أن تستخدم نفس الأسلوب الذي استخدمناه في إجابة السؤال الأول لكي تسترجع الألوان بالطريقة التي تريدها
ولتنفيذ المثال افتح مشروع وأضف له PictureBox ثم أضف له ListBox وقم بإضافة صورة واستخدمها داخل المشروع ثم اكتب الكود بالشكل التالي
كود :
Public Class Form1
Dim bmp As Bitmap = New Bitmap(My.Resources.imgsrv)
Dim ColorArray As New System.Collections.ArrayList()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim clr As Color
For y As Integer = 0 To bmp.Height - 1
For x As Integer = 0 To bmp.Width - 1
clr = bmp.GetPixel(x, y)
If Not ColorArray.Contains(clr) Then ColorArray.Add(clr)
Next
Next
For Each isColor As Color In ColorArray
ListBox1.Items.Add(isColor)
ListBox1.Sorted = True
Next
Dim c As Integer = ColorArray.Count
Me.Text = c.ToString
PictureBox1.Image = bmp
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
For i As Integer = 0 To ColorArray.Count - 1
Me.BackColor = ListBox1.SelectedItem
Next
End Sub
End Classطبعا من الممكن الاستغناء عن استخدام المصفوفات وبالتالي استرجاع الألوان مباشرة ووضعها في ListBox
والكود التالي يوضح ذلك وعليك أن تلاحظ عزيزي القارئ استخدام جملة IF لتجنب تكرار المدخلات الي ListBox وعليك أن تلاحظ أيضا المتغير c وهو عبارة عن Integer الذي يعطينا مباشرة عدد الألوان الموجودة بالصورة
كود :
Public Class Form1
Dim bmp As Bitmap = New Bitmap(My.Resources.imgsrv)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim clr As Color
For y As Integer = 0 To bmp.Height - 1
For x As Integer = 0 To bmp.Width - 1
clr = bmp.GetPixel(x, y)
If Not ListBox1.Items.Contains(clr) Then ListBox1.Items.Add(clr)
Next
Next
ListBox1.Sorted = True
Dim c As Integer = ListBox1.Items.Count
Me.Text = c.ToString
PictureBox1.Image = bmp
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Me.BackColor = ListBox1.SelectedItem
End Sub
End Classكود :
Public Shared Function GetColorsInBitmap(ByVal bitmap As Bitmap) As ArrayList
Dim ColorArray As New System.Collections.ArrayList()
Dim clr As Color
For y As Integer = 0 To bitmap.Height - 1
For x As Integer = 0 To bitmap.Width - 1
clr = bitmap.GetPixel(x, y)
If Not ColorArray.Contains(clr) Then ColorArray.Add(clr)
Next
Next
Return ColorArray
End Functionولتنفيذ المثال افتح مشروع وأضف له PictureBox ثم أضف له ListBox وقم بإضافة صورة واستخدمها داخل المشروع ثم اكتب الكود بالشكل التالي
كود :
Public Class Form1
Dim bmp As Bitmap = New Bitmap(My.Resources.imgsrv)
Dim Arr As ArrayList
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Arr = GetColorsInBitmap(bmp)
For Each isColor As Color In Arr
ListBox1.Items.Add(isColor)
ListBox1.Sorted = True
Next
Dim c As Integer = Arr.Count
Me.Text = c.ToString
PictureBox1.Image = bmp
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
For i As Integer = 0 To Arr.Count - 1
Me.BackColor = ListBox1.SelectedItem
Next
End Sub
Public Shared Function GetColorsInBitmap(ByVal bitmap As Bitmap) As ArrayList
Dim ColorArray As New System.Collections.ArrayList()
Dim clr As Color
For y As Integer = 0 To bitmap.Height - 1
For x As Integer = 0 To bitmap.Width - 1
clr = bitmap.GetPixel(x, y)
If Not ColorArray.Contains(clr) Then ColorArray.Add(clr)
Next
Next
Return ColorArray
End Function
End Class