0

I am drawing a rectangle on a image which is within a picture box. The problem I have is that the rectangle is drawn behind the image. Please see the picture attached.

How can I draw on top of the image?

My on paint of the picture is as below. My paint is on my picturebox - I wonder if that is the problem ? but there is no paint on the image?

Rectangle ZoomRect1 = new Rectangle(Math.Min(ZoomToRectangleLeftButtonLocation.X, ZoomToRectangleCurrentButtonLocation.X),
                                               Math.Min(ZoomToRectangleLeftButtonLocation.Y, ZoomToRectangleCurrentButtonLocation.Y),
                                               Math.Abs(ZoomToRectangleLeftButtonLocation.X - ZoomToRectangleCurrentButtonLocation.X),
                                               Math.Abs(ZoomToRectangleLeftButtonLocation.Y - ZoomToRectangleCurrentButtonLocation.Y));


Graphics g1 = e.Graphics;
                Pen pen = new Pen(Color.Red, 2);
                g1.DrawRectangle(pen, ZoomRect1);
                pen.Dispose();

Image with picture box

user2837961
  • 1,505
  • 3
  • 27
  • 67
  • Do have a look at [this](https://stackoverflow.com/questions/27337825/picturebox-paintevent-with-other-method/27341797?s=3|20.8032#27341797) post! – TaW Jun 08 '18 at 12:03

2 Answers2

2

I believe you are using OnPaint method of the form ! Not the pictureBox and that's why the rectangle is in form. To draw rec on PictureBox you have to do like :

private void pictureBox1_Paint(object sender, PaintEventArgs e)
   {

    Rectangle ee = new Rectangle(10, 10, 30, 30);
    using (Pen pen = new Pen(Color.Red, 2))
        {
            e.Graphics.DrawRectangle(pen, ee);
        }

    }

And here the graphics e is for the pictureBox not the form itself.

EDIT

If the first answer didn't help : try this out.

Add this method where ever you want and call it

 public void paintOnPictureBox()
        {
            Rectangle ee = new Rectangle(10, 10, 50, 50);
            Graphics gr = Graphics.FromImage(pictureBox1.Image);
            using (Pen pen = new Pen(Color.Green, 2))
            {
                gr.DrawRectangle(pen, ee);
            }
        }

DON'T FORGET : on pictureBox OnPaint event, add this line :

this.Refresh();

and the results :

enter image description here

EDIT 2 :

It's bad to add

this.Refresh();

in the onPaint method since it may cause other components to flickering. As it slows the form on showing and operating !

It's better to add it in the end of painting method like :

public void paintOnPictureBox()
        {
            Rectangle ee = new Rectangle(10, 10, 50, 50);
            Graphics gr = Graphics.FromImage(pictureBox1.Image);
            using (Pen pen = new Pen(Color.Green, 2))
            {
                gr.DrawRectangle(pen, ee);
            }
this.Refresh();
        }
Kaj
  • 806
  • 6
  • 16
  • I am definitely using picturebox paint – user2837961 Jun 08 '18 at 11:45
  • _I am definitely using picturebox paint_ : This seems doubtful. Better show the whole Paint event! And make sure it is hooked up to the PBox and not the Form! All drawing is over Image and BackgroundImage. (And below any neseted Controls.) – TaW Jun 08 '18 at 12:00
  • I have edit the answer with other method. Tested and works – Kaj Jun 08 '18 at 12:03
0

I guess you are setting image source to control

Try to draw image, than rectangle

Graphics g1 = e.Graphics;
g1.DrawImage(...);
Pen pen = new Pen(Color.Red, 2);
g1.DrawRectangle(pen, ZoomRect1);
pen.Dispose();
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184