المشاركات : 2,523
المواضيع 707
الإنتساب : Sep 2012
السمعة :
48
الشكر: 91
تم شكره 1104 مرات في 297 مشاركات
كاتب الموضوع : 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
الأن لنبني دالة من ألمثال رقم 1 أعلاه ولنطلق عليها اسم GetImageColorsCount
كود :
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
مثال 2 استخدام List (Of )
لتنفيذ المثال: افتح مشروع جديد وأضف له 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
الأن لنبني دالة من ألمثال رقم 2 أعلاه ولنطلق عليها اسم GetImageColorsCount
كود :
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
مثال 3 استخدام HashSet
لتنفيذ المثال: افتح مشروع جديد وأضف له 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
الأن لنبني دالة من ألمثال رقم 3 أعلاه ولنطلق عليها اسم GetImageColorsCount
كود :
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
عموما وفي النهاية من الممكن استخدام أي دالة تريدها من الأمثلة الثلاثة أعلاه أو أن تصنع داله أخري شبيهة بأي أسلوب تراه مناسبا وعموما الهدف النهائي واحد وهو بناء دالة نستطيع من خلالها الحصول علي عدد الألوان الموجودة بأي صورة
المشاركات : 2,523
المواضيع 707
الإنتساب : Sep 2012
السمعة :
48
الشكر: 91
تم شكره 1104 مرات في 297 مشاركات
هل من الممكن استرجاع الألوان الموجودة في الصورة ؟
وهنا سوف نستخدم نفس الفكرة أو نفس الأسلوب الذي استخدمناه في السؤال السابق وكما تلاحظ عزيزي القارئ أن الأمر يتكرر ولكنه يأخذ أشكالا أخري فقط.
إذن باستخدام جملة 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
عليك عزيزي القارئ أن تلاحظ المتغير C وهو عبارة عن Integer وهو هنا سوف يسترجع عدد الألوان الموجود في Array بدون استخدام الدالة التي صنعناها سابقا والخاصة باسترجاع عدد الألوان الموجودة بصورة ما وستلاحظ أيضا أن الكود تقريبا لم يتغير كثيرا ولكننا قمنا بعمل تعديل بسيط جدا حتى نسترجع الألوان الموجودة بالصورة
طبعا من الممكن الاستغناء عن استخدام المصفوفات وبالتالي استرجاع الألوان مباشرة ووضعها في 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
الأن وكالمعتاد لنبني دالة من ألمثال الموجود بالكود أعلاه وسنطلق عليها إسم GetColorsInBitmap وهنا هدفنا هو أن نسترجع مصفوفة بها الألوان الموجودة بأي صورة
كود :
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
المثال التالي يوضح كيفية استخدام الدالة GetColorsInBitmap
ولتنفيذ المثال افتح مشروع وأضف له 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
المشاركات : 2,523
المواضيع 707
الإنتساب : Sep 2012
السمعة :
48
الشكر: 91
تم شكره 1104 مرات في 297 مشاركات
هل من الممكن إعادة تلوين الصورة ؟
إعادة تلوين الصورة يتم باستخدام GetPixel وأيضا SetPixel ثم تلوين الصورة بأي لون نحدده مسبقا أو عن طريق استخدام أي لون موجود بالفعل داخل الصورة وهنا يمكننا أن نطلق علي هذا الأسلوب الجزء السهل في عملية إعادة تلوين الصورة
كما يمكن أيضا استخدام ColorMatrix Class في إعادة تلوين الصورة وهذا أسلوب أخر لتغيير ألوان الصورة وسنناقشه لاحقا في المقال
ومن الأساليب التي من الممكن إتباعها أيضا وهو يتم باستخدام BitmapData Class و Bitmap Class معا وهنا علينا أن ندرس ونفهم مسميات مثل Scan0 و Stride وأيضا LockBits و UnlockBits وأيضا علينا أن نفهم كيفية التعامل مع Memory وكيفية القراءة منها والكتابة إليها عن طريق استخدام Marshal Class وهذا ما يمكن أن نطلق عليه الجزء الصعب في عملية إعادة تلوين الصورة وعلي العموم هذا الأسلوب يحتاج لمقال خاص به حتى يمكن توضيحه بشكل أفضل
ولنعطي بعض الأمثلة عن كيفية استخدام GetPixel و SetPixel في كيفية تلوين الصورة لكي يتضح الأمر للقارئ
مثال رقم 1
افتح مشروع جديد وأضف له صورة ثم أضف الي الفورم PictureBox ثم اكتب الكود بالشكل التالي
كود :
Public Class Form1
Dim bmp As New Bitmap(My.Resources.imgsrv)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For x As Integer = 0 To bmp.Width - 1
For y As Integer = 0 To bmp.Height - 1
Dim bmpColor As Color = bmp.GetPixel(x, y)
Dim clr As Color = Color.FromArgb(100, Color.Blue)
bmp.SetPixel(x, y, clr)
Next
Next
PictureBox1.Image = bmp
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
End Class
ستلاحظ عزيزي القارئ عند تنفيذ الكود أن الصورة قد اختفت تماما ولكنها أخذت اللون الذي حددناه وهو قيمة المتغير clr
ستلاحظ أيضا أن هناك لونان موجودان بالكود وهما المتغير bmpColor وهو يمثل مجموعة الألوان الموجودة بالصورة وأيضا المتغير clr وهو يمثل اللون الذي حددناه ثم قمنا باستخدام SetPixel لتغيير ألوان البكسل الموجودة بالصورة الي اللون الجديد
إذن ماذا نفعل لكي تظهر الصورة. لنأخذ مثالا أخر لتوضيح ذلك
مثال رقم 2
افتح مشروع جديد وأضف له صورة ثم أضف الي الفورم PictureBox ثم اكتب الكود بالشكل التالي
كود :
Public Class Form1
Dim bmp As 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 = Color.FromArgb(100, Color.Blue)
For x As Integer = 0 To bmp.Width - 1
For y As Integer = 0 To bmp.Height - 1
Dim bmpColor As Color = bmp.GetPixel(x, y)
Dim newclr As Color = Color.FromArgb(100, clr.R, bmpColor.B, clr.B)
bmp.SetPixel(x, y, newclr)
Next
Next
PictureBox1.Image = bmp
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
End Class
ستلاحظ عزيزي القارئ عند تنفيذ الكود أن الصورة الأن أصبحت ظاهرة للعين ولكن ما الفارق بين الكود الموجود بالمثال رقم 1 والكود الموجود بالمثال رقم 2. الفارق أننا قمنا بتعريف لون ثالث وهو المتغير newclr حيث مكونات هذا اللون عبارة عن جزئين وهم اللون الذي حددناه وهو يمثل المتغير clr وأيضا اللون الخاص بالصورة ويمثله المتغير bmpColor
الأن حاول تجربة الأمثلة التالية ثم عليك عزيزي القارئ أن تلاحظ الفرق فيما بينهما
مثال رقم 3
افتح مشروع جديد وأضف له صورة ثم أضف الي الفورم PictureBox ثم اكتب الكود بالشكل التالي
كود :
Public Class Form1
Dim bmp As New Bitmap(My.Resources.imgsrv)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For x As Integer = 0 To bmp.Width - 1
For y As Integer = 0 To bmp.Height - 1
Dim bmpColor As Color = bmp.GetPixel(x, y)
Dim newclr As Color = Color.FromArgb(0, 0, bmpColor.B)
bmp.SetPixel(x, y, newclr)
Next
Next
PictureBox1.Image = bmp
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
End Class
في المثال رقم 3 أعلاه استخدمنا فقط الألوان الموجودة بالصورة حيث قمنا بإلغاء جميع الألوان الحمراء والخضراء بالصورة واسترجعنا فقط اللون الأزرق ثم استخدمناه في إعادة تلوين الصورة
مثال رقم 4
افتح مشروع جديد وأضف له صورة ثم أضف الي الفورم PictureBox ثم اكتب الكود بالشكل التالي
كود :
Public Class Form1
Dim bmp As New Bitmap(My.Resources.imgsrv)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For x As Integer = 0 To bmp.Width - 1
For y As Integer = 0 To bmp.Height - 1
Dim bmpColor As Color = bmp.GetPixel(x, y)
Dim newclr As Color = Color.FromArgb(0, bmpColor.G, 0)
bmp.SetPixel(x, y, newclr)
Next
Next
PictureBox1.Image = bmp
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
End Class
في المثال رقم 4 أعلاه استخدمنا فقط الألوان الموجودة بالصورة حيث قمنا بإلغاء جميع الألوان الحمراء والزرقاء بالصورة واسترجعنا فقط اللون الأخضر ثم استخدمناه في إعادة تلوين الصورة
مثال رقم 5
افتح مشروع جديد وأضف له صورة ثم أضف الي الفورم PictureBox ثم اكتب الكود بالشكل التالي
كود :
Public Class Form1
Dim bmp As New Bitmap(My.Resources.imgsrv)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For x As Integer = 0 To bmp.Width - 1
For y As Integer = 0 To bmp.Height - 1
Dim bmpColor As Color = bmp.GetPixel(x, y)
Dim newclr As Color = Color.FromArgb(bmpColor.R, 0, 0)
bmp.SetPixel(x, y, newclr)
Next
Next
PictureBox1.Image = bmp
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
End Class
في المثال رقم 5 أعلاه استخدمنا فقط الألوان الموجودة بالصورة حيث قمنا بإلغاء جميع الألوان الخضراء والزرقاء بالصورة واسترجعنا فقط اللون الأحمر ثم استخدمناه في إعادة تلوين الصورة
مثال رقم 6
افتح مشروع جديد وأضف له صورة ثم أضف الي الفورم PictureBox ثم اكتب الكود بالشكل التالي
كود :
Dim bmp As New Bitmap(My.Resources.imgsrv)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For x As Integer = 0 To bmp.Width - 1
For y As Integer = 0 To bmp.Height - 1
Dim bmpColor As Color = bmp.GetPixel(x, y)
Dim newclr As Color = Color.FromArgb(0, bmpColor.G, bmpColor.B)
bmp.SetPixel(x, y, newclr)
Next
Next
PictureBox1.Image = bmp
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
End Class
في المثال رقم 6 أعلاه استخدمنا فقط الألوان الموجودة بالصورة حيث قمنا بإلغاء اللون الأحمر فقط واسترجعنا الألوان التي مكوناتها خليط من اللونين الأزرق والأخضر حيث تم استخدامهم في إعادة تلوين الصورة
حاول أن تجعل القيمة الخاصة بالمتغير newclr تكون بالشكل التالي ستكتشف أنك لو قمت بذلك ستجد أن الصورة لم تتغير علي نهائيا
كود :
Dim newclr As Color = Color.FromArgb(bmpColor.R, bmpColor.G, bmpColor.B)
حاول أيضا أن تجعل القيمة الخاصة بالمتغير newClr تأخذ واحدا من القيم التالية الموجودة بالكود أدناه ستكتشف أن ألوان الصورة تحولت الي الأبيض والأسود مع الوضع في الاعتبار أن ألوان الصورة الأصلية ستؤثر علي كثافة اللونين الأبيض والأسود
كود :
Dim newClr As Color = Color.FromArgb(bmpColor.R, bmpColor.R, bmpColor.R)
أو الشكل التالي
Dim newClr As Color = Color.FromArgb(bmpColor.G, bmpColor.G, bmpColor.G)
أو الشكل التالي
Dim newClr As Color = Color.FromArgb(bmpColor.B, bmpColor.B, bmpColor.B)
حاول أيضا أن تجعل القيمة الخاصة بالمتغير newClr تساوي القيمة التالية الموجودة بالكود أدناه ستكتشف أن ألوان الصورة قد تم عكسها تمام
كود :
Dim clr As Color = Color.FromArgb(255, 255 - bmpColor.R, 255 - bmpColor.G, 255 - bmpColor.B)
المشاركات : 2,523
المواضيع 707
الإنتساب : Sep 2012
السمعة :
48
الشكر: 91
تم شكره 1104 مرات في 297 مشاركات
إذن ومن مجموعة الأكواد السابقة نكتشف أنه يمكننا أن نقوم بتغيير ألوان الصورة الي ما نريد وعلي سبيل المثال يمكننا أن نعكس الألوان أو نجعل الصورة أبيض وأسود أو نجعل ألوان الصورة حمراء الي أخره من التغييرات التي نريدها.
وكالمعتاد من الممكن إنشاء الكثير من الدوال من الأمثلة أعلاه و مجموعة الأكواد التالية توضح شكل الدوال التي من الممكن بناؤها من الكود أعلاه طبعا يمكنك أن تبني أكثر من دالة عزيزي القارئ عموما الأمر متروك لخيالك
كود :
Public Shared Function RevertColor(ByVal color As Color) As Color
Dim clr As Color = color.FromArgb(255, 255 - color.R, 255 - color.G, 255 - color.B)
RevertColor = clr
End Function
Public Shared Function RevertColor(ByVal color As Color, ByVal Alpha As Integer) As Color
If Alpha < 10 OrElse Alpha > 255 Then
Throw New ArgumentOutOfRangeException("Alpha must be between 10 and 255.")
End If
Dim clr As Color = color.FromArgb(255, 255 - color.R, 255 - color.G, 255 - color.B)
RevertColor = clr
End Function
Public Shared Function RevertImageColors(ByVal bitmap As Bitmap) As Bitmap
For x As Integer = 0 To bitmap.Width - 1
For y As Integer = 0 To bitmap.Height - 1
Dim bmpColor As Color = bitmap.GetPixel(x, y)
Dim newclr As Color = Color.FromArgb(bmpColor.G, bmpColor.B, bmpColor.R)
bitmap.SetPixel(x, y, newclr)
Next
Next
Dim bmp As New Bitmap(bitmap.Width, bitmap.Height, bitmap.PixelFormat)
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawImage(bitmap, 0, 0)
Return bmp
End Function
Public Shared Function RevertImageColors(ByVal bitmap As Bitmap, ByVal color As Color) As Bitmap
Dim clr As Color = color.FromArgb(255, color)
For x As Integer = 0 To bitmap.Width - 1
For y As Integer = 0 To bitmap.Height - 1
Dim bmpColor As Color = bitmap.GetPixel(x, y)
Dim newclr As Color = color.FromArgb(clr.R, bmpColor.G, clr.B)
bitmap.SetPixel(x, y, newclr)
Next
Next
Dim bmp As New Bitmap(bitmap.Width, bitmap.Height, bitmap.PixelFormat)
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawImage(bitmap, 0, 0)
Return bmp
End Function
Public Shared Function RevertImageColors(ByVal bitmap As Bitmap, ByVal color As Color, ByVal Alpha As Integer) As Bitmap
If Alpha < 50 OrElse Alpha > 255 Then
Throw New ArgumentOutOfRangeException("Alpha must be between 50 and 255.")
End If
Dim clr As Color = color.FromArgb(Alpha, color)
For x As Integer = 0 To bitmap.Width - 1
For y As Integer = 0 To bitmap.Height - 1
Dim bmpColor As Color = bitmap.GetPixel(x, y)
Dim newclr As Color = color.FromArgb(clr.A, clr.R, bmpColor.G, clr.B)
bitmap.SetPixel(x, y, newclr)
Next
Next
Dim bmp As New Bitmap(bitmap.Width, bitmap.Height, bitmap.PixelFormat)
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawImage(bitmap, 0, 0)
Return bmp
End Function
Public Shared Function TurnImageToGray(ByVal bitmap As Bitmap) As Bitmap
For x As Integer = 0 To bitmap.Width - 1
For y As Integer = 0 To bitmap.Height - 1
Dim bmpColor As Color = bitmap.GetPixel(x, y)
Dim newclr As Color = Color.FromArgb(bmpColor.R, bmpColor.R, bmpColor.R)
bitmap.SetPixel(x, y, newclr)
Next
Next
Dim bmp As New Bitmap(bitmap.Width, bitmap.Height, bitmap.PixelFormat)
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawImage(bitmap, 0, 0)
Return bmp
End Function
Public Shared Function TurnImageToGray(ByVal bitmap As Bitmap, ByVal Transperent As Boolean) As Bitmap
If Transperent Then
For x As Integer = 0 To bitmap.Width - 1
For y As Integer = 0 To bitmap.Height - 1
Dim bmpColor As Color = bitmap.GetPixel(x, y)
Dim newclr As Color = Color.FromArgb(100, bmpColor.R, bmpColor.R, bmpColor.R)
bitmap.SetPixel(x, y, newclr)
Next
Next
Else
For x As Integer = 0 To bitmap.Width - 1
For y As Integer = 0 To bitmap.Height - 1
Dim bmpColor As Color = bitmap.GetPixel(x, y)
Dim newclr As Color = Color.FromArgb(bmpColor.R, bmpColor.R, bmpColor.R)
bitmap.SetPixel(x, y, newclr)
Next
Next
End If
Dim bmp As New Bitmap(bitmap.Width, bitmap.Height, bitmap.PixelFormat)
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawImage(bitmap, 0, 0)
Return bmp
End Function
Public Shared Function TurnImageToMonoChrome(ByVal bitmap As Bitmap) As Bitmap
Dim bmp As New Bitmap(bitmap.Width, bitmap.Height, bitmap.PixelFormat)
For x As Integer = 0 To bmp.Width - 1
For y As Integer = 0 To bmp.Height - 1
Dim bmpColor As Color = bitmap.GetPixel(x, y)
Dim a As Integer = CInt(bmpColor.R * 0.3 + bmpColor.G * 0.59 + bmpColor.B * 0.11)
bmp.SetPixel(x, y, Color.FromArgb(a, a, a))
Next
Next
Return bmp
End Function
Public Shared Function TurnImageToGray(ByVal bitmap As Bitmap, ByVal Transperent As Boolean, ByVal Alpha As Integer) As Bitmap
If Alpha < 50 OrElse Alpha > 255 Then
Throw New ArgumentOutOfRangeException("Alpha must be between 50 and 255.")
End If
If Transperent Then
For x As Integer = 0 To bitmap.Width - 1
For y As Integer = 0 To bitmap.Height - 1
Dim bmpColor As Color = bitmap.GetPixel(x, y)
Dim newclr As Color = Color.FromArgb(Alpha, bmpColor.R, bmpColor.R, bmpColor.R)
bitmap.SetPixel(x, y, newclr)
Next
Next
Else
For x As Integer = 0 To bitmap.Width - 1
For y As Integer = 0 To bitmap.Height - 1
Dim bmpColor As Color = bitmap.GetPixel(x, y)
Dim newclr As Color = Color.FromArgb(bmpColor.R, bmpColor.R, bmpColor.R)
bitmap.SetPixel(x, y, newclr)
Next
Next
End If
Dim bmp As New Bitmap(bitmap.Width, bitmap.Height, bitmap.PixelFormat)
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawImage(bitmap, 0, 0)
Return bmp
End Function
الكود التالي يوضح كيفية استخدام احدي الدوال الموجودة اعلاه وذلك لإعطاء فكرة عن كيفية استخدام هذه الدوال
كود :
Public Class Form1
Dim bmp As New Bitmap(My.Resources.imgsrv)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim bmp1 As Bitmap = TurnImageToGray(bmp, True, 70)
PictureBox1.Image = bmp1
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
Public Shared Function TurnImageToGray(ByVal bitmap As Bitmap, ByVal Transperent As Boolean, ByVal Alpha As Integer) As Bitmap
If Alpha < 50 OrElse Alpha > 255 Then
Throw New ArgumentOutOfRangeException("Alpha must be between 50 and 255.")
End If
If Transperent Then
For x As Integer = 0 To bitmap.Width - 1
For y As Integer = 0 To bitmap.Height - 1
Dim bmpColor As Color = bitmap.GetPixel(x, y)
Dim newclr As Color = Color.FromArgb(Alpha, bmpColor.R, bmpColor.R, bmpColor.R)
bitmap.SetPixel(x, y, newclr)
Next
Next
Else
For x As Integer = 0 To bitmap.Width - 1
For y As Integer = 0 To bitmap.Height - 1
Dim bmpColor As Color = bitmap.GetPixel(x, y)
Dim newclr As Color = Color.FromArgb(bmpColor.R, bmpColor.R, bmpColor.R)
bitmap.SetPixel(x, y, newclr)
Next
Next
End If
Dim bmp As New Bitmap(bitmap.Width, bitmap.Height, bitmap.PixelFormat)
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawImage(bitmap, 0, 0)
Return bmp
End Function
End Class
بالتوفيق
أخوكم عمر
|