02-10-12, 03:21 PM
كاتب الموضوع : silverlight
التعامل مع الصور Images في بيئة الدوت نت باستخدام +GDI اللغة المستخدمة: الفيجوال بيسك
التطبيق: فيجوال استوديو 2005 و 2008
المستوي: التقييم متروك للقارئ
إعداد: مهندس / عمر أمين إبراهيم
الجزء الثامن: التعامل مع الألوان في الصورة
قد يخطر في خاطر القارئ أنه يريد أن يستفيد قليلا من الألوان الموجودة بأي صورة إذن كيف نفعل ذلك؟ إذن لنضع مجموعة من الأسئلة ثم نحاول الإجابة عليها. في واقع الأمر هناك الكثير من الأسئلة التي من الممكن أن تدور حول هذا الجزء من المقال الذي نحاول فيه أن نتعرف علي الألوان في أي صورة
هل من الممكن معرفة عدد ألوان الصورة؟
الفكرة هنا هي استخدام جملة For…..Next لقراءة عدد الألوان الموجودة بصورة ما ثم نضيفها الي مصفوفة ثم نسترجع العدد الإجمالي للألوان من المصفوفة وهنا نحن نسترجع Integer وهناك الكثير من الأساليب التي من الممكن استخدامها في ذلك واعتقد System.Collections Class و أيضا به الكثير والكثير من الأشياء التي من الممكن أن تعطينا ما نريد وسوف نوضح ذلك بأكثر من مثال وفي النهاية كل الطرق التي من الممكن أن نستخدمها سوف تؤدي الهدف المطلوب ولسوف
مثال 1 استخدام ArrayList
لتنفيذ المثال: افتح مشروع جديد وأضف له PictureBox ثم عليك تعريف صورة ما واستخدم الكود التالي. لاحظ فقط استخدام IF لتجنب تكرار المدخلات
كود :
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 ColorArray As New System.Collections.ArrayList()
Dim clr As Integer
For y As Integer = 0 To bmp.Height - 1
For x As Integer = 0 To bmp.Width - 1
clr = bmp.GetPixel(x, y).ToArgb()
If Not ColorArray.Contains(clr) Then ColorArray.Add(clr)
Next
Next
Dim c As Integer = ColorArray.Count
Me.Text = c.ToString
PictureBox1.Image = bmp
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
End Classكود :
Public Shared Function GetImageColorsCount(ByVal bitmap As Bitmap) As Integer
Dim ColorArray As New System.Collections.ArrayList()
Dim clr As Integer
For y As Integer = 0 To bitmap.Height - 1
For x As Integer = 0 To bitmap.Width - 1
clr = bitmap.GetPixel(x, y).ToArgb()
If Not ColorArray.Contains(clr) Then ColorArray.Add(clr)
Next
Next
GetImageColorsCount = ColorArray.Count
End Functionلتنفيذ المثال: افتح مشروع جديد وأضف له PictureBox ثم عليك تعريف صورة ما واستخدم الكود التالي. لاحظ فقط استخدام IF لتجنب تكرار المدخلات
كود :
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 ColorList As New System.Collections.Generic.List(Of Integer)
Dim clr As Integer
For y As Integer = 0 To bmp.Height - 1
For x As Integer = 0 To bmp.Width - 1
clr = bmp.GetPixel(x, y).ToArgb()
If Not ColorList.Contains(clr) Then ColorList.Add(clr)
Next
Next
Dim d As Integer = ColorList.Count
Me.Text = d.ToString
PictureBox1.Image = bmp
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
End Classكود :
Public Shared Function GetImageColorsCount(ByVal bitmap As Bitmap) As Integer
Dim ColorList As New System.Collections.Generic.List(Of Integer)
Dim clr As Integer
For y As Integer = 0 To bitmap.Height - 1
For x As Integer = 0 To bitmap.Width - 1
clr = bitmap.GetPixel(x, y).ToArgb()
If Not ColorList.Contains(clr) Then ColorList.Add(clr)
Next
Next
GetImageColorsCount = ColorList.Count
End Functionلتنفيذ المثال: افتح مشروع جديد وأضف له PictureBox ثم عليك تعريف صورة ما واستخدم الكود التالي. لاحظ فقط أمما لم نستخدم IF لتجنب تكرار المدخلات حيث والسبب هو أن HashSet يمنع تكرار المدخلات اتوماتيكيا وهذا المثال يمكن تنفيذه فقط مع الفيجوال دوت نت 2008 حيث أن الكلاس HashSet موجود فقط في الدوت نت 3.5
وهذا الكلاس رائع فعلا واتمني أنكم تدرسوه جيدا
كود :
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 ColorSet As New System.Collections.Generic.HashSet(Of Integer)
For y As Integer = 0 To bmp.Height - 1
For x As Integer = 0 To bmp.Width - 1
ColorSet.Add(bmp.GetPixel(x, y).ToArgb())
Next
Next
Dim b As Integer = ColorSet.Count
Me.Text = b.ToString
PictureBox1.Image = bmp
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
End Classكود :
Public Shared Function GetImageColorsCount(ByVal bitmap As Bitmap) As Integer
Dim ColorSet As New System.Collections.Generic.HashSet(Of Integer)
For y As Integer = 0 To bmp.Height - 1
For x As Integer = 0 To bmp.Width - 1
ColorSet.Add(bmp.GetPixel(x, y).ToArgb())
Next
Next
GetImageColorsCount = ColorSet.Count
End Functionوالمثال التالي يوضح كيفية استخدام الدالة GetImageColorCount
كود :
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
PictureBox1.Image = bmp
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
Dim b As Integer = GetImageColorsCount(PictureBox1.Image)
Me.Text = b.ToString
End Sub
Public Shared Function GetImageColorsCount(ByVal bitmap As Bitmap) As Integer
Dim ColorSet As New System.Collections.Generic.HashSet(Of Integer)
For y As Integer = 0 To bitmap.Height - 1
For x As Integer = 0 To bitmap.Width - 1
ColorSet.Add(bitmap.GetPixel(x, y).ToArgb())
Next
Next
GetImageColorsCount = ColorSet.Count
End Function
End Class