目前,我在使用
SetPixel()
方法来更改位图中每个像素的颜色。对于小尺寸的图片,这种方法效果良好,但是当我在大图像上测试时,处理时间会比较长。由于我之前没有在VB.Net中处理图像,所以可能忽略了一些明显的问题。我正在编写一个将图像转换为灰度的程序。这确实可以产生正确的结果,但速度较慢,并且在此期间UI会冻结,因此我希望尽可能提高转换速度。以下是我的当前代码: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