代码之家  ›  专栏  ›  技术社区  ›  Jarad

PictureBox仅刷新一次[重复]

  •  -1
  • Jarad  · 技术社区  · 7 年前

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

    试图实时显示摄像头中的图像。程序初始化后,picturebox应开始显示图像(请参阅图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.Invalidate(); 
    

      pictureBox.Invalidate()
      pictureBox.Update();
    

    但这并没有解决问题

    2 回复  |  直到 7 年前
        1
  •  0
  •   Barr J    7 年前

    你需要的是 PictureBox.Refresh() 它继承自control类。

    刷新而不是更新:

    //pictureBox.Update();
    pictureBox.Refresh();
    
        2
  •  0
  •   Jarad    7 年前

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

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