PictureBox只刷新一次

时间:2017-12-26 04:35:12

标签: c# image winforms picturebox pixel

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

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

但问题是,当我放回物体时,我应该能够获得与picture1类似的图像,但它看起来像picture2。

我想通过调用pictureBox.Refresh()它会自动重绘/重绘图像?但它似乎并没有得到适当的刷新。

    // 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)这是我启动程序时得到的图像 enter image description here

(图2)这是我删除对象后的图像 enter image description here

我尝试用

交换pictureBox.Refresh()
  pictureBox.Invalidate(); 

  pictureBox.Invalidate()
  pictureBox.Update();

但它没有解决问题

2 个答案:

答案 0 :(得分:0)

您需要的是PictureBox.Refresh(),它是从控件类继承而来的。

而不是更新,刷新:

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

答案 1 :(得分:0)

我使程序在绘制从缓冲区获得的图像之前绘制整个图片框白色。尽管

,这可能不是解决此问题的最有效方法
    // 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;
    }