Drawing a circle on an image

17,327

Solution 1

reference: Answer by @Albin at draw-an-arrow-on-a-picturebox

public partial class Form1 : Form
{
    private bool isMoving = false;
    private Point mouseDownPosition = Point.Empty;
    private Point mouseMovePosition = Point.Empty;
    private Dictionary<Point, Point> Circles = new Dictionary<Point, Point>();
    public Form1()
    {
        InitializeComponent();

        // 
        // pictureBox1
        //             
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(231, 235);
        this.pictureBox1.TabIndex = 0;
        this.pictureBox1.TabStop = false;
        this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
        this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
        this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
        this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
        this.Controls.Add(this.pictureBox1);
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        Pen p = new Pen(Color.Red);
        var g = e.Graphics;
        if (isMoving)
        {
            g.Clear(pictureBox1.BackColor);
            g.DrawEllipse(p, new Rectangle(mouseDownPosition, new Size(mouseMovePosition.X - mouseDownPosition.X, mouseMovePosition.Y - mouseDownPosition.Y)));
            foreach (var circle in Circles)
            {
                g.DrawEllipse(p, new Rectangle(circle.Key, new Size(circle.Value.X - circle.Key.X, circle.Value.Y - circle.Key.Y)));
            }
        }
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        pictureBox1.Cursor = Cursors.Cross;
        isMoving = true;
        mouseDownPosition = e.Location;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (isMoving)
        {
            mouseMovePosition = e.Location;
            pictureBox1.Invalidate();
        }
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        pictureBox1.Cursor = Cursors.Default;
        if (isMoving)
        {
            Circles.Add(mouseDownPosition, mouseMovePosition);
        }
        isMoving = false;
    }
}

Solution 2

Sure take the graphic instance of the control anddraw on it . It will be reflected only when its repainted or Invalidated. Use DrawEllipse Method of Graphic Class to draw circle.

here is the code:

Graphics gf = picturebox1.CreateGraphics();
//A circle with Red Color and 2 Pixel wide line
gf.DrawEllipse(new Pen(Color.Red, 2),new Rectangle(0, 0, 200, 200));

Invalidate it to actually draw it on the control (picturebox)

picturebox1.Invalidate();

It will draw a circle 200 pixel in diameter

Share:
17,327
Bosco
Author by

Bosco

Updated on June 27, 2022

Comments

  • Bosco
    Bosco almost 2 years

    I have a program for image segmentation in WinForms Application C#, and I have an image loaded in a pictureBox. I need to draw a small circle(or an ellipse, it does not matter) on that image (inside a region of interest, and allowing it to grow outwards until it reaches the desired boundary).

    And the question is how can I draw that circle anywhere on that image? (and if it`s possible to draw that circle in a different color, red for example)

    Thank you.

  • Bosco
    Bosco over 13 years
    I get this error: 'System.Drawing.Rectangle' is a 'type' but is used like a 'variable'
  • Shekhar_Pro
    Shekhar_Pro over 13 years
    Oops Sorry forgot that new Keyword before Rectangle.. Edited my code
  • Bosco
    Bosco over 13 years
    and this creates a circle in the upper left corner, and I want to draw that circle myself...
  • Shekhar_Pro
    Shekhar_Pro over 13 years
    You ARE creating the circle yourself...in the Rectangle(0, 0, 200, 200).. First argument is X pos on the control and Second is Y on the control then 3rd Argument is width and 4th is height. Just specify the XY where you want its top left corner to be... If you want drawing the circle point by point by some algo then use Draw path with GraphicsPath object and fill it with points from your algo... I wouldn't go that way... It seems you are really beginner to this then i would suggest you to go read some good books or look at MSDN Documentations.. I learned it that way
  • Bosco
    Bosco over 13 years
    Thank you very much...this worked for drawing the ellipse in the pictureBox...but when I want to draw ON a picture that I previously loaded in that pictureBox, it only appears the ellipse...Is it possible that BOTH the ELLIPSE and the PICTURE to be on the same time in the pictureBox? And if yes, then it's possible to animate that ellipse to work on the picture? (I draw that ellipse inside a region of interest and I need it to grow outwards until it reaches the desired boundary of that region of interest). If not...what other option do I have?
  • Bosco
    Bosco over 13 years
    I've done it myself, I didn't saw "g.Clear(pictureBox1.BackColor); "...delete that and it's all ok...