开发者

Drawing a circle on an image

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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜