PictureBox not refreshing properly?

匆匆过客 提交于 2019-12-20 07:12:30

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/47937061/picturebox-not-refreshing-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!