4

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
Wolf
  • 9,679
  • 7
  • 62
  • 108
Piratica
  • 483
  • 2
  • 11

1 Answers1

5

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.

Private Function GrayedImage(orgBMP As Bitmap) As Bitmap

    Dim grayscale As New Imaging.ColorMatrix(New Single()() _
        {New Single() {0.3, 0.3, 0.3, 0, 0},
         New Single() {0.59, 0.59, 0.59, 0, 0},
         New Single() {0.11, 0.11, 0.11, 0, 0},
         New Single() {0, 0, 0, 1, 0},
         New Single() {0, 0, 0, 0, 1}})

    Dim bmpTemp As New Bitmap(orgBMP)
    Dim grayattr As New Imaging.ImageAttributes()
    grayattr.SetColorMatrix(grayscale)

    Using g As Graphics = Graphics.FromImage(bmpTemp)
        g.DrawImage(bmpTemp, New Rectangle(0, 0, bmpTemp.Width, bmpTemp.Height), 
                    0, 0, bmpTemp.Width, bmpTemp.Height, 
                    GraphicsUnit.Pixel, grayattr)
    End Using

    Return bmpTemp
End Function

Values are rounded from .299, .587, .114

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • time is ~80ms for a 1280*720 image. I dont have a problem with that considering what it is doing. – Ňɏssa Pøngjǣrdenlarp May 11 '14 at 17:24
  • Thanks, I've never even known about `ColorMatrix` before, and looking into it I've discovered that it is incredibly complicated. However the results are brilliant, I got **1028 milliseconds (~1 second)** on a **3008 x 2000** image, whereas I _was_ getting **297273 milliseconds (4 mins 57 seconds)** on the same image. Clearly a massive improvement, I'll just have to read [this](http://msdn.microsoft.com/en-us/library/6tf7sa87(v=vs.110).aspx) until I get it. – Piratica May 11 '14 at 18:32