At the moment, I am using the SetPixel()
method to change the colour of every pixel in a bitmap. This works fine on small images with small dimensions, but when I test it on large images it does take a while.
I haven't worked with images in VB.Net before, so I might just be overlooking something obvious. I'm doing this to make a program which converts an image to grey scale. This produces the right result but at a low speed, and during this time the UI freezes, so I'm keen to maximize the speed of conversion.
This is my code at the moment:
Dim tmpImg As New Bitmap(img) '"img" is a reference to the original image
For x As Integer = 0 To tmpImg.Width - 1
For y As Integer = 0 To tmpImg.Height - 1
Dim clr As Byte
With tmpImg.GetPixel(x, y)
clr = ConvertToGrey(.R, .G, .B)
End With
tmpImg.SetPixel(x, y, Color.FromArgb(clr, clr, clr))
Next
Next
Private Function ConvertToGrey(ByVal R As Byte, ByVal G As Byte, ByVal B As Byte) As Byte
Return (0.2126 * R) + (0.7152 * B) + (0.0722 * G)
End Function
Fast is a relative term, but this will convert a 480x270 image to greyscale in 10-12 ms (obviously system dependent) which does not seem unduly long. I'm quite sure it will be faster than SetPixel.
Values are rounded from .299, .587, .114