I was searching the net for changing the image colors to its gray scale
I found a good example here
But It changes the background color also
to it's black-white,So it appears in black color.
With simple a change in code to fix the problem
here it is:
Public Function ConvertToGrayScale(ByVal Source As Bitmap) As Bitmap
Dim bm As New Bitmap(Source.Width, Source.Height)
Dim x As IntegerDim y As Integer
Dim FirstPixelColor As Color
FirstPixelColor = Source.GetPixel(0, 0)For y = 0 To bm.Height - 1
For x = 0 To bm.Width - 1
Dim c As Color = Source.GetPixel(x, y)
If c <> FirstPixelColor Then
Dim luma As Integer = CInt(c.R * 0.3 + c.G * 0.59 + c.B * 0.11)
bm.SetPixel(x, y,Color.FromArgb(luma, luma, luma)) Else
bm.SetPixel(x, y,Color.FromArgb(0, 0, 0, 0))
End If
Next
Next
Return bm
End Function
Just copy and paste and try it ,If you have a simpler method tell me.
| Color to grayScale with Transparent Background |
But It changes the background color also
to it's black-white,So it appears in black color.
With simple a change in code to fix the problem
here it is:
Public Function ConvertToGrayScale(ByVal Source As Bitmap) As Bitmap
Dim bm As New Bitmap(Source.Width, Source.Height)
Dim x As IntegerDim y As Integer
Dim FirstPixelColor As Color
FirstPixelColor = Source.GetPixel(0, 0)For y = 0 To bm.Height - 1
For x = 0 To bm.Width - 1
Dim c As Color = Source.GetPixel(x, y)
If c <> FirstPixelColor Then
Dim luma As Integer = CInt(c.R * 0.3 + c.G * 0.59 + c.B * 0.11)
bm.SetPixel(x, y,Color.FromArgb(luma, luma, luma)) Else
bm.SetPixel(x, y,Color.FromArgb(0, 0, 0, 0))
End If
Next
Next
Return bm
End Function
Just copy and paste and try it ,If you have a simpler method tell me.
No comments:
Post a Comment