02-10-12, 03:23 PM
إذن ومن مجموعة الأكواد السابقة نكتشف أنه يمكننا أن نقوم بتغيير ألوان الصورة الي ما نريد وعلي سبيل المثال يمكننا أن نعكس الألوان أو نجعل الصورة أبيض وأسود أو نجعل ألوان الصورة حمراء الي أخره من التغييرات التي نريدها.
وكالمعتاد من الممكن إنشاء الكثير من الدوال من الأمثلة أعلاه و مجموعة الأكواد التالية توضح شكل الدوال التي من الممكن بناؤها من الكود أعلاه طبعا يمكنك أن تبني أكثر من دالة عزيزي القارئ عموما الأمر متروك لخيالك
الكود التالي يوضح كيفية استخدام احدي الدوال الموجودة اعلاه وذلك لإعطاء فكرة عن كيفية استخدام هذه الدوال
بالتوفيق
أخوكم عمر
وكالمعتاد من الممكن إنشاء الكثير من الدوال من الأمثلة أعلاه و مجموعة الأكواد التالية توضح شكل الدوال التي من الممكن بناؤها من الكود أعلاه طبعا يمكنك أن تبني أكثر من دالة عزيزي القارئ عموما الأمر متروك لخيالك
كود :
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بالتوفيق
أخوكم عمر
