在图像上绘制一个圆 [英] Drawing a circle on an image

查看:123
本文介绍了在图像上绘制一个圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WinForms应用程序C#程序的图像分割,而且我有一个图片加载图像。我需要画一个小圆圈(或椭圆形,也没有关系),该图像上(感兴趣的区域内,并允许它,直到它到达所期望的边界向外生长)。<​​/ P>

和问题是我怎么能得出这个圈子的任何地方形象? (如果它`可能得出该圆用不同的颜色,红色例如)

感谢你。

解决方案

参考:按@Albin在回答<一href="http://stackoverflow.com/questions/4255614/draw-an-arrow-on-a-picturebox-in-c/4255732#4255732">draw-an-arrow-on-a-picturebox

 公共部分类Form1中:形态
{
    私人布尔isMoving = FALSE;
    私人点mouseDownPosition = Point.Empty;
    私人点mouseMovePosition = Point.Empty;
    私人字典&LT;点,点&GT;圈=新字典&LT;点,点&GT;();
    公共Form1中()
    {
        的InitializeComponent();

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

    私人无效pictureBox1_Paint(对象发件人,PaintEventArgs的E)
    {
        笔P =新钢笔(Color.Red);
        VAR G = e.Graphics;
        如果(isMoving)
        {
            g.Clear(pictureBox1.BackColor);
            g.DrawEllipse(P,新的Rectangle(mouseDownPosition,新的大小(mouseMovePosition.X  -  mouseDownPosition.X,mouseMovePosition.Y  -  mouseDownPosition.Y)));
            的foreach(在圈子VAR圆)
            {
                g.DrawEllipse(P,新的Rectangle(circle.Key,新的大小(circle.Value.X  -  circle.Key.X,circle.Value.Y  -  circle.Key.Y)));
            }
        }
    }

    私人无效pictureBox1_MouseDown(对象发件人,发送MouseEventArgs E)
    {
        pictureBox1.Cursor = Cursors.Cross;
        isMoving = TRUE;
        mouseDownPosition = e.Location;
    }

    私人无效pictureBox1_MouseMove(对象发件人,发送MouseEventArgs E)
    {
        如果(isMoving)
        {
            mouseMovePosition = e.Location;
            pictureBox1.Invalidate();
        }
    }

    私人无效pictureBox1_MouseUp(对象发件人,发送MouseEventArgs E)
    {
        pictureBox1.Cursor = Cursors.Default;
        如果(isMoving)
        {
            Circles.Add(mouseDownPosition,mouseMovePosition);
        }
        isMoving = FALSE;
    }
}
 

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.

解决方案

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

这篇关于在图像上绘制一个圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆