1

I'm fairly new to using C#. I started on monday to be honest and prior to this only had basic MATLAB level of experience, so I'm still some kind of newb scrub. The question I will ask has already been asked, according to my googling skills, but I have yet to find a working solution.

I'm currently writing a program to make the image acquisition from a Vimba camera, and the subsequent image tratment, and that program includes a UI, so a form1. On this form, there is a pictureBoxLiveCamera that shows the live recording from the camera.

What I am trying to do is :

  • draw two rectangles on top of the pictureBoxLiveCamera, indicating specific zones of the image (THIS WORKS LIKE A CHARM)
  • have those two rectangles NOT drawn when I launch the program, but when I click a specific drawRectanglesButton (AND HERE I HAVE A PROBLEM)
  • have that same button hide the rectangles if they are drawn, and then draw them if they are hidden (I'M NOT THERE YET)

I have already explored a lot of similar threads, and this one particularly shaped the way I wrote the code, though it is not working as intended yet.

Here are the protions of my code that are relevant here :

  • form loading :

        // Form loading
    private void Form1_Load(object sender, EventArgs e)
    {
        pictureBoxLiveCamera.Paint += pictureBoxLiveCamera_Paint;
    ... //Vimba API startup and so on
    }
    
  • button code :

        private void drawRectanglesButton_Click(object sender, EventArgs e)
    {
        this.Invalidate(); // force Redraw the form
    }
    
  • rectangles drawing code :

        public void pictureBoxLiveCamera_Paint(object sender, PaintEventArgs e)
    {
        Rectangle ee = new Rectangle(-5, 120, 790, 100);
        using (Pen pen = new Pen(Color.Red, 2))
        {
            e.Graphics.DrawRectangle(pen, ee);
        }
        Rectangle eee = new Rectangle(-5, 364, 790, 100);
        using (Pen pen = new Pen(Color.Red, 2))
        {
            e.Graphics.DrawRectangle(pen, eee);
        }
    }
    

My problem is the following : as it is, when I start the program, the rectangles are already drawn, so the button does nothing.

I'm pretty sure the mistake will be obvious to most of you, but this has been driving me crazy all day.

Thanks a lot !

Trion

Trion
  • 115
  • 11
  • You need to add a field to store what / whether you want to draw. Set that field when you click the button and check it in `Paint` – SLaks Mar 30 '18 at 15:04
  • 1
    Simply add a *bool* variable to your class. Set it to true in the Click event handler. – Hans Passant Mar 30 '18 at 15:04
  • Thanks @HansPassant. Here is what I tried : bool drawRectanglesButtonClicked = false; private void drawRectanglesButton_Click(object sender, EventArgs e) { drawRectanglesButtonClicked = true; this.Invalidate(); // force Redraw the form } Unfortunately, this didn't solve my issue. Did I misunderstood your answer ? (Sorry I couldn't get the formatting right :/ ) – Trion Mar 30 '18 at 15:18
  • You of course also want to test this variable in your painting code. Add the if() statement. – Hans Passant Mar 30 '18 at 15:19
  • Thanks a lot @HansPassant, I added the if() condition in the paint even, and replaced .Invalidate() with .Refresh(), and this is working ! – Trion Mar 30 '18 at 15:27

0 Answers0