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 Color)
pseudoColors(x, y) = value
End Set
End Property
Public ReadOnly Property Palette As Color(,)
Get
Return pseudoColors
End Get
End Property
Public Shared Narrowing Operator CType(value As Image) As Picture
Dim result As Picture = New Picture(value.Height, value.Width)
Dim bmp As Bitmap = New Bitmap(value)
For i As Integer = 0 To bmp.Height - 1
For j As Integer = 0 To bmp.Width - 1
result(i, j) = bmp.GetPixel(j, i)
Next
Next
Return result
End Operator
Public Shared Widening Operator CType(ps As Picture) As Bitmap
Dim bmp As Bitmap = New Bitmap(ps.Width, ps.Height)
For i As Integer = 0 To ps.Height - 1
For j As Integer = 0 To ps.Width - 1
bmp.SetPixel(j, i, ps(i, j))
Next
Next
Return bmp
End Operator
End Class
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 Color)
pseudoColors(x, y) = value
End Set
End Property
Public ReadOnly Property Palette As Color(,)
Get
Return pseudoColors
End Get
End Property
Public Shared Narrowing Operator CType(value As Image) As Picture
Dim result As Picture = New Picture(value.Height, value.Width)
Dim bmp As Bitmap = New Bitmap(value)
For i As Integer = 0 To bmp.Height - 1
For j As Integer = 0 To bmp.Width - 1
result(i, j) = bmp.GetPixel(j, i)
Next
Next
Return result
End Operator
Public Shared Widening Operator CType(ps As Picture) As Bitmap
Dim bmp As Bitmap = New Bitmap(ps.Width, ps.Height)
For i As Integer = 0 To ps.Height - 1
For j As Integer = 0 To ps.Width - 1
bmp.SetPixel(j, i, ps(i, j))
Next
Next
Return bmp
End Operator
End Class
الكود التالي يوضح كيف تستخدم الكلاس اعلاه
' تعريف الصورة
Dim img As Bitmap = My.Resources._11
' تحويل الصورة الي الكلاس اعلاه
Dim pic As Picture = CType(img, Picture)
Dim pic As Picture = CType(img, Picture)
' عرض الصورة
PictureBox1.Image = CType(pic, Bitmap)
PictureBox1.Image = CType(pic, Bitmap)
Comments
Post a Comment