【问题标题】:PictureBox not refreshing properly?PictureBox 没有正确刷新?
【发布时间】:2018-06-04 20:16:59
【问题描述】:

如果我问一个愚蠢的问题,请原谅我是编程新手。

我正在尝试显示从实时摄像机获得的实时图像。当我启动程序时,图片框能够显示对象(参见图片1)。当我移除对象时,它会显示此图像(请参阅图片2)。但问题是,当我放回对象时,我应该能够获得与图片 1 相似但看起来像图片 2 的图像。 是不是因为pictureBox没有正常刷新?

    //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;
    }

(图1)这是我启动程序时得到的图像

(图2)这是我移除物体后的图像

在我看来,一旦“黑色”被画到图片框上,它似乎就无法消失了。

【问题讨论】:

    标签: c# image winforms picturebox pixel


    【解决方案1】:

    您需要将所有的绘图逻辑放在图片框的绘制事件中。当此事件触发时,所有内容都将被重绘。手动引发此调用 picturebox.Invalidate()。

    因此,将 drawPix 内容放入绘制事件中,并在您的按钮单击中使用 picturebox.Invalidate() 强制图片框刷新。

    【讨论】:

    • 好的,我会尝试的,如果有效,我会投票。非常感谢!
    • 对于drawPix部分,是这样的吗?
    • private void pictureBox_Paint(object sender, PaintEventArgs e, int x, int y, int r, int g, int b) { ((Bitmap)pictureBox.Image).SetPixel(x, y, Color .FromArgb(r, g, b));返回; }
    猜你喜欢
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多