hi everyone .I have this code to convert color image to grayscale image but the problem is that the image after converting does not display on the picturebox2 . I am using visualbasic.net 2010

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim pic As Bitmap = New Bitmap(PictureBox1.Image)
        Dim x As Integer
        Dim y As Integer
        x = pic.Width
        y = pic.Height
        Dim gray = New Bitmap(pic.Width, pic.Height)
        Dim red As Integer
        Dim green As Integer
        Dim blue As Integer
        For x = 0 To (pic.Width) - 1
            For y = 0 To (pic.Height) - 1
                Dim r As Color = pic.GetPixel(x, y)
                red = r.R
                green = r.G
                blue = r.B
                Dim d As Integer
                d = CInt((red + green + blue) / 3)
                Dim c1 As Integer = CInt(Math.Round(d))
                gray.SetPixel(x, y, Color.FromArgb(c1, c1, c1))
                PictureBox2.Image = gray
            Next
        Next

    End Sub

There is no reason why this code shouldn't work. To speed things up a little move PictureBox2.Image = gray outside of the two for-next loops.

try creating a new form, with a picturebox (Change no properties) and the button. place the code in the button and load an image into the picture box's image property and try running again.

I've just copied and pasted your code into the above scenario and it works.

Try

Dim pic As Bitmap = New Bitmap(PictureBox1.Image)
Dim x As Integer = pic.Width
Dim y As Integer = pic.Height
Dim gray = New Bitmap(pic.Width, pic.Height)

For x = 0 To (pic.Width) - 1
    For y = 0 To (pic.Height) - 1
        Dim c As Color = pic.GetPixel(x, y)
        Dim r As Integer = c.R
        Dim g As Integer = c.G
        Dim b As Integer = c.B
        Dim d As Integer = (r + g + b) \ 3
        gray.SetPixel(x, y, Color.FromArgb(d, d, d))
    Next
Next

PictureBox2.Image = gray

I just don't get this one Jim, apart from a whole bunch of un-needed code... batoolhussain's, yours, and even:

        Dim pic As Bitmap = New Bitmap(PictureBox1.Image)
        Dim gray = New Bitmap(pic.Width, pic.Height)

        For x As Integer = 0 To (pic.Width) - 1
            For y As Integer = 0 To (pic.Height) - 1
                Dim c As Color = pic.GetPixel(x, y)
                Dim d As Integer = (CInt(c.R) + CInt(c.G) + CInt(c.B)) \ 3
                gray.SetPixel(x, y, Color.FromArgb(d, d, d))
            Next
        Next

        PictureBox2.Image = gray

Should all work. I can only assume there's a dodgy property somewhere?

Incidentally, rather than averaging the colors I read somewhere that you get a better result from

Dim r As Single = c.R
Dim g As Single = c.G
Dim b As Single = c.B
Dim d As Integer = CInt(r * 0.3 + g * 0.59 + b * 0.11)

@jc - I think the only problem was, as you pointed out, that the last statement should not have been inside the loop.Perhaps that just slowed everything down to the point where it looked like nothing was being done. There is an example here that uses a FormatConvertedBitmap. I played with it for a bit then gave up.

Thx for ur answer.i already try it and its WORK!!!thx sir.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.