-1

Decided to re-post with the actual code i am using

Attempting to display image in real time from a camera. When the program is initialized, the picturebox should start displaying the image(Refer to picture1). When i remove an object, the image i get is (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.

I thought by calling pictureBox.Refresh() it will automatically repaint/redraw the image? But it doesnt seems like it is refreshing properly.

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

(Picture1)This is the image i get when i start the program enter image description here

(Picture2)This is the image after i remove the object enter image description here

I tried swapping out the pictureBox.Refresh() with

  pictureBox.Invalidate(); 

or

  pictureBox.Invalidate()
  pictureBox.Update();

but it didnt solve the problem

Jarad
  • 41
  • 1
  • 9
  • It's his question that he posted again with the full code, he mentioned it above. Please read the questions first O_O – Barr J Dec 26 '17 at 06:03
  • 1
    @BarrJ: Plutonix _did_ read the question first, that's how they could tell that this is an exact duplicate of the other question. If the OP wants to improve a post, the way to do that is to _improve that post_, not post the exact same question a second time. – Peter Duniho Dec 26 '17 at 08:48

2 Answers2

0

What you need, is PictureBox.Refresh() which is inherited from control class.

instead of update, refresh:

//pictureBox.Update();
pictureBox.Refresh();
Barr J
  • 10,636
  • 1
  • 28
  • 46
0

I make the program to draw entire picturebox white before drawing the image obtained from the buffer. This might not be the most efficient way to solve this problem though

    // 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;
    }
Jarad
  • 41
  • 1
  • 9