图片框只刷新一次

PictureBox only refresh once

决定用我正在使用的实际代码重新post

正在尝试实时显示来自相机的图像。当程序初始化时,图片框应该开始显示图像(参考图片1)。当我删除一个对象时,我得到的图像是(参考图片2)。

但问题是,当我把对象放回去时,我应该能够得到一个类似于图片1但看起来像图片2的图像。

我以为通过调用 pictureBox.Refresh() 它会自动 repaint/redraw 图像?但它似乎并没有正确刷新。

    // R Mode Tab
    private void RModeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        timer1.Enabled = true;
    }

    // Timer for R mode
    private void timer1_Tick(object sender, EventArgs e)
    {
        // Grab buffer from camera
        camera.grab(out buffer);

        accessRMode(buffer);

        pictureBox.Refresh();

        // Release buffer       
        camera.Release();
    }

    // For accessing R Mode
    private void accessRMode(buffer)
    {
        int numberOfScansR = buffer.Height;
        bitmapHeight = numberOfScansR;

        // Loop through all scans in the buffer.
        int CompWidth = buffer.Components["R mode"].Format.Width;

        bitmapWidth = CompWidth;

        // Get pointer to beginning of scan number 'scan' of R mode
        ushort[,] data = buffer.Components["R mode"].GetRows<ushort>(0, numberOfScansR);

        for (int scan = 0; scan < numberOfScansR; scan++)
        {
            // Loop through all elements in each scan.
            for (int col = 0; col < CompWidth; col++)
            {
                ushort val = data[scan, col];
                if (val != 0)
                {
                    sumR += val;
                    val = (ushort)(val / 257);
                    drawpix(col, scan, (int)val, (int)val, (int)val);
                    countR++;
                }
            }
        }
    }

    // Draw pixel method
    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)这是我移除对象后的图像

我尝试将 pictureBox.Refresh() 替换为

  pictureBox.Invalidate(); 

  pictureBox.Invalidate()
  pictureBox.Update();

但是没有解决问题

您需要的是 PictureBox.Refresh(),它继承自控件 class。

不是更新,而是刷新:

//pictureBox.Update();
pictureBox.Refresh();

我让程序在绘制从缓冲区获得的图像之前将整个图片框绘制成白色。这可能不是解决这个问题的最有效方法

    // Draw white method
    public void draw_white(buffer)
    {
        int numberOfScansR = buffer.Height;
        bitmapHeight = numberOfScansR;

        int subCompWidth = buffer.Components["R"].Format.Width;
        bitmapWidth = subCompWidth;

        for (int scan = 0; scan < numberOfScansR; scan++)
        {
            for (int col = 0; col < subCompWidth; col++)
            {
                // Draw the entire picturebox white color
                drawPix(col, scan, (int)255, (int)255, (int)255);
            }
        }
    }

    // R Mode Tab
    private void RModeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        timer1.Enabled = true;
    }

    // Timer for R mode
    private void timer1_Tick(object sender, EventArgs e)
    {
        // Grab buffer from camera
        camera.grab(out buffer);

        draw_white(buffer);
        pictureBox.Invalidate();
        pictureBox.Update();

        accessRMode(buffer); 
        pictureBox.Invalidate();
        pictureBox.Update();


        // Release buffer       
        camera.Release();
    }

    // For accessing R Mode
    private void accessRMode(buffer)
    {
        int numberOfScansR = buffer.Height;
        bitmapHeight = numberOfScansR;

        // Loop through all scans in the buffer.
        int CompWidth = buffer.Components["R mode"].Format.Width;

        bitmapWidth = CompWidth;

        // Get pointer to beginning of scan number 'scan' of R mode
        ushort[,] data = buffer.Components["R mode"].GetRows<ushort>(0, 
        numberOfScansR);

        for (int scan = 0; scan < numberOfScansR; scan++)
        {
            // Loop through all elements in each scan.
            for (int col = 0; col < CompWidth; col++)
            {
                ushort val = data[scan, col];
                if (val != 0)
                {
                    sumR += val;
                    val = (ushort)(val / 257);
                    drawpix(col, scan, (int)val, (int)val, (int)val);
                    countR++;
                }
            }
        }
    }

    // Draw pixel method
    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;
    }