02-10-12, 03:24 PM
كاتب الموضوع : silverlight
التعامل مع الصور Images في بيئة الدوت نت باستخدام +GDI اللغة المستخدمة: الفيجوال بيسك
التطبيق: فيجوال استوديو 2005 و 2008
المستوي: التقييم متروك للقارئ
إعداد: مهندس / عمر أمين إبراهيم
الجزء التاسع: التعامل مع الألوان في الصورة باستخدام Color Matrix
كيف نتحكم في ألوان الصورة عن طريق استخدام ColorMatrix Class أو من الممكن أن نطلق عليها مصفوفة الألوان
وهي مصفوفة بها مجموعة من المعاملات Coefficients وهذه المعاملات عبارة عن 5 أعمدة في 5 صفوف تتراوح قيمتها بين الرقمين صفر و 1 ولو قمنا بالتغيير أو بالتعديل علي قيمة هذه المعاملات سوف نستطيع التغيير في ألوان الصورة وبخاصة أن أي صورة يكون شكل المصفوفة لها كما هو موضح بالكود التالي وهو الشكل الطبيعي لأي صورة بدون تعديلات في ألوانها أي انك لو استخدمت المصفوفة بالشكل الموضح أدناه فلن يحدث تعديل للصورة وبالتبعية يمكن استغلال هذه الكود لكي تسترجع الصورة الأصلية في أي وقت تشاء
كود :
Dim cm As System.Drawing.Imaging.ColorMatrix = _
New System.Drawing.Imaging.ColorMatrix(New Single()() _
{New Single() {1, 0, 0, 0, 0}, _
New Single() {0, 1, 0, 0, 0}, _
New Single() {0, 0, 1, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
ويتم التعامل مع ألوان الصورة هنا عن طريق تحديد ColorMatrix معينة ثم تمريرها إلي ImageAttributes ثم استخدام الأخير في عملية رسم الصورة بالألوان الجديدة هذه هي الفكرة ببساطة والمثال التالي يوضح كيفية عمل ذلك
ولتنفيذ المثال افتح مشروع جديد ثم قم بتعريف صورة ثم أضف الي الفورم PictureBox ثم اكتب الكود بالشكل التالي ثم بعد ذلك حاول تغيير القيم للمتغير Red و Blue و Green و Alphaعلي التوالي بأي أرقام تريدها بشرط أن لا تتعدي قيمتهم الرقم 1 ولا تقل عن الرقم صفر وهنا ستكتشف أن ما يفعله الكود التالي قد فعلناه سابقا باستخدام GetPixel و SetPixel
كود :
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 Red As Single = 0.9
Dim Green As Single = 0.9
Dim Blue As Single = 0.9
Dim Alpha As Single = 1
Dim w As Single = 1
Dim cm As System.Drawing.Imaging.ColorMatrix = _
New System.Drawing.Imaging.ColorMatrix(New Single()() _
{New Single() {1, 0, 0, 0, 0}, _
New Single() {0, 1, 0, 0, 0}, _
New Single() {0, 0, 1, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {Red, Green, Blue, Alpha, w}})
Dim imageAttr As New System.Drawing.Imaging.ImageAttributes
imageAttr.SetColorMatrix(cm)
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawImage(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttr)
PictureBox1.Image = bmp
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
End Class
ولتنفيذ المثال افتح مشروع جديد ثم قم بتعريف صورة ثم أضف الي الفورم PictureBox ثم اكتب الكود بالشكل التالي ثم بعد ذلك حاول تغيير القيم للمتغير Red و Blue و Green و Alphaعلي التوالي بأي أرقام تريدها بشرط أن لا تتعدي قيمتهم الرقم 1 ولا تقل عن الرقم صفر وهنا ستكتشف أن ما يفعله هذا الكود التالي قد فعلناه سابقا باستخدام GetPixel و SetPixel أيضا وتحديدا الكود الخاص بالدالة TurnImageToMonoChrom 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 Red As Single = 0.3
Dim Green As Single = 0.11
Dim Blue As Single = 0.59
Dim Alpha As Single = 1
Dim w As Single = 1
Dim cm As System.Drawing.Imaging.ColorMatrix = _
New System.Drawing.Imaging.ColorMatrix(New Single()() _
{New Single() {Red, Red, Red, 0, 0}, _
New Single() {Blue, Blue, Blue, 0, 0}, _
New Single() {Green, Green, Green, 0, 0}, _
New Single() {0, 0, 0, Alpha, 0}, _
New Single() {0, 0, 0, 0, w}})
Dim imageAttr As New System.Drawing.Imaging.ImageAttributes
imageAttr.SetColorMatrix(cm)
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawImage(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttr)
PictureBox1.Image = bmp
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
End Class
إذن ومن مجموعة الأكواد السابقة نكتشف أيضا أنه يمكننا أن نقوم بتغيير ألوان الصورة الي ما نريد وعلي سبيل المثال يمكننا أن نعكس الألوان أو نجعل الصورة أبيض وأسود أو نجعل ألوان الصورة حمراء الي أخره من التغييرات التي نريدها.وأنا شخصيا أفضل التعامل مع GetPixel و SetPixel وأنت عزيزي القارئ عليك أن تختار الطريقة التي تريدها وهذا متروك لك
وكالمعتاد من الممكن إنشاء الكثير من الدوال من الأمثلة أعلاه و مجموعة الأكواد التالية توضح شكل الدوال التي من الممكن بناؤها من الكود أعلاه طبعا يمكنك عزيزي القارئ أن تبني أكثر من دالة عموما الأمر متروك لخيالك
كود :
Public Shared Function RevertImageColors(ByVal bitmap As Bitmap, ByVal R As Single, ByVal G As Single, ByVal B As Single) As Bitmap
Dim Red As Single = R / 255
Dim Green As Single = G / 255
Dim Blue As Single = B / 255
Dim cm As System.Drawing.Imaging.ColorMatrix = _
New System.Drawing.Imaging.ColorMatrix(New Single()() _
{New Single() {1, 0, 0, 0, 0}, _
New Single() {0, 1, 0, 0, 0}, _
New Single() {0, 0, 1, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {Red, Green, Blue, 0, 1}})
Dim imageAttr As New System.Drawing.Imaging.ImageAttributes
imageAttr.SetColorMatrix(cm)
Dim gr As Graphics = Graphics.FromImage(bitmap)
gr.DrawImage(bitmap, New Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, imageAttr)
Return bitmap
End Function
Public Shared Function RevertImageToMono(ByVal bitmap As Bitmap) As Bitmap
Dim cm As System.Drawing.Imaging.ColorMatrix = _
New System.Drawing.Imaging.ColorMatrix(New Single()() _
{New Single() {0.3, 0.3, 0.3, 0, 0}, _
New Single() {0.59, 0.59, 0.59, 0, 0}, _
New Single() {0.11, 0.11, 0.11, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
Dim imageAttr As New System.Drawing.Imaging.ImageAttributes
imageAttr.SetColorMatrix(cm)
Dim gr As Graphics = Graphics.FromImage(bitmap)
gr.DrawImage(bitmap, New Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, imageAttr)
Return bitmap
End Function
Public Shared Function RevertImageToNegative(ByVal bitmap As Bitmap) As Bitmap
Dim cm As System.Drawing.Imaging.ColorMatrix = _
New System.Drawing.Imaging.ColorMatrix(New Single()() _
{New Single() {-1, 0, 0, 0, 0}, _
New Single() {0, -1, 0, 0, 0}, _
New Single() {0, 0, -1, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
Dim imageAttr As New System.Drawing.Imaging.ImageAttributes
imageAttr.SetColorMatrix(cm)
Dim gr As Graphics = Graphics.FromImage(bitmap)
gr.DrawImage(bitmap, New Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, imageAttr)
Return bitmap
End Function
ولنعطي مثالا علي استخدام إحدي الدوال الموجودة اعلاه ولتنفيذ المثال افتح مشروع جديد ثم قم بتعريف صورة ثم أضف له 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
PictureBox1.Image = RevertImageColors(bmp, 120, 190, 150)
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
Public Shared Function RevertImageColors(ByVal bitmap As Bitmap, ByVal R As Single, ByVal G As Single, ByVal B As Single) As Bitmap
Dim Red As Single = R / 255
Dim Green As Single = G / 255
Dim Blue As Single = B / 255
Dim cm As System.Drawing.Imaging.ColorMatrix = _
New System.Drawing.Imaging.ColorMatrix(New Single()() _
{New Single() {1, 0, 0, 0, 0}, _
New Single() {0, 1, 0, 0, 0}, _
New Single() {0, 0, 1, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {Red, Green, Blue, 0, 1}})
Dim imageAttr As New System.Drawing.Imaging.ImageAttributes
imageAttr.SetColorMatrix(cm)
Dim gr As Graphics = Graphics.FromImage(bitmap)
gr.DrawImage(bitmap, New Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, imageAttr)
Return bitmap
End Function
End Class
بالتوفيق
أخوكم عمر