Jarad
Jarad

Reputation: 51

PictureBox not refreshing properly?

I am new to programming pardon me if i am asking a dumb question.

I am trying to display real time image that i obtained from a live camera. When i start the program, the picturebox is able to show the object(refer to picture1). When i remove the object, it display this image(refer to picture2). But the problem is that when i put back the object, i should be able to get an image that is similar to picture1 but instead it look like picture2. Is it because the pictureBox is not refreshing properly?

    //R Mode Tab
    private void RModeToolStripMenuItem_Click(object sender, EventArgs e)
    {

        // There is a method, which will obtain the data value and pass to this drawpix
        drawPix(x, y, (int)data, (int)data, (int)data);

        pictureBox.Refresh();

        // Release camera buffer
        camera.Release();
    }

    private void drawPix(int x, int y, int r, int g, int b)
    {
        ((Bitmap)pictureBox.Image).SetPixel(x, y, Color.FromArgb(r, g, b));
        return;
    }

(Picture1)This is the image i get when i start the program

(Picture2)This is the image after i remove the object

To me it seems like once "black" is drawn to the pictureBox, it doesnt seems to be able to go away.

Upvotes: 0

Views: 852

Answers (1)

Felix Almesberger
Felix Almesberger

Reputation: 901

You need to put all all of your drawing logic in the paint event of the picturebox. Everything will be redrawn when this event fires. To manually raise this call picturebox.Invalidate().

So put the drawPix stuff into the paint event and force the picturebox to refresh using picturebox.Invalidate() in your button click.

Upvotes: 1

Related Questions