Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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)

Modification:

I only managed to use this function for now e.Graphics.DrawPath but I want to draw by hand a CIRCLE or an ELLIPSE, and this function doesn't satisfy me. This is the code that does the drawing. I also have a button(btnLoad) and a pictureBox(pcbImage) where the image is loaded.
Here's my code:
C#
namespace WindowsFormsApplication1
{
    public partial class frmMain : Form
    {
        private System.Drawing.Brush m_objBrush;
        private Graphics m_objGraphics;
        private Point lastPoint = Point.Empty;
        private System.Drawing.Drawing2D.GraphicsPath mousePath;
        private int mouseX, mouseY;
        Point mouseDownLocation;
        public frmMain()
        {
            mousePath = new System.Drawing.Drawing2D.GraphicsPath();
           InitializeComponent();
        }
        private void btnQuit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void frmMain_Load(object sender, EventArgs e)
        {
            m_objGraphics = this.CreateGraphics();
        }
        private void pcbImage_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDownLocation = new Point(e.X, e.Y);
            mousePath.StartFigure();
            pcbImage.Focus();
            pcbImage.Invalidate();
        }
        private void pcbImage_MouseUp(object sender, MouseEventArgs e)
        {
            pcbImage.Invalidate();
        }
        private void pcbImage_MouseMove(object sender, MouseEventArgs e)
        {
            mouseX = e.X;
            mouseY = e.Y;
            if (e.Button == MouseButtons.Left && e.Button == Button.MouseButtons)
            {
                mousePath.AddLine(mouseX, mouseY, mouseX, mouseY);
                pcbImage.Invalidate();
            }
        }
        private void pcbImage_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawPath(System.Drawing.Pens.Red, mousePath);
        }
    }
}


Thank you.
Posted
Updated 22-Jan-11 6:31am
v2

You can get a Graphics reference to the image by using the Graphics.FromImage() function. Then I would use the function in the Graphics class called DrawEllipse[^]. It has an overload that allows you to pass a Pen (i.e. Pens.Red) and a Rectangle. The rectangle will define the location and the size that identify where and how big to make the ellipse.
C#
Graphics gfx = Graphics.FromImage ( tempImage );
gfx.DrawEllipse ( Pens.Red, new Rectangle ( tempPoint, tempSize ) );


[EDIT]
Here is a basic example of how to do it, it assumes that you have a form with a PictureBox on it. You will need to modify this to suit your needs, but this will help you understand what the process is:
C#
public partial class Form1 : Form
{
    // this is the variable we will use to control where the circle/ellipse is drawn.
    // we are going to use the mouse events to set the properties of this variable.
    Rectangle circle_bounds = new Rectangle ();

    public Form1 ()
    {
        InitializeComponent ();
    }

    private void Form1_Load ( object sender, EventArgs e )
    {
        // load an image into your PictureBox
        this.pictureBox1.Load ( @"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg" );
    }

    private void pictureBox1_MouseDown ( object sender, MouseEventArgs e )
    {
        // we are setting the location of the rectangle to
        // the position of the mouse when the button is pushed down
        this.circle_bounds.Location = e.Location;
    }

    private void pictureBox1_MouseUp ( object sender, MouseEventArgs e )
    {
        // the next line will set the size of the circle/eliipse based on
        // where the mouse was when the button was released
        this.circle_bounds.Size = new Size ( e.X - this.circle_bounds.X, e.Y - this.circle_bounds.Y );

        // next is where we are actually drawing onto the image:
        // 1. Get a Graphics representation of the image
        using ( Graphics gfx = Graphics.FromImage ( this.pictureBox1.Image ) )
        {
            // 2. Draw an ellipse onto the image using the rectangle we created as the bounds
            gfx.DrawEllipse ( Pens.Red, this.circle_bounds );

            // 3. Refresh the PictureBox so that it shows the circle/ellipse we just drew
            this.pictureBox1.Refresh ();
        }
    }
}


[/EDIT]
 
Share this answer
 
v3
Comments
bosco_boom 21-Jan-11 19:42pm    
and this I have to put in

private void pictureBox1_Click(object sender, EventArgs e)

or in

private void pictureBox1_Paint(object sender, EventArgs e)
JOAT-MON 21-Jan-11 21:22pm    
Sounds like what you want is to use the MouseDown event to get the starting point of the rectangle. Then use the MouseUp event to get the ending point. The difference between these is the size of the rectangle. You can put it in a number of different events or functions, depending on how you are using it, but to get the feel for it I would start by putting it in the MouseUp.
Manfred Rudolf Bihy 22-Jan-11 12:23pm    
Moved from OP's answer:

And how can I draw myself that circle. For now I managed to draw a circle in the upper left corner.
JOAT-MON 22-Jan-11 23:13pm    
Thanks, Manfred! I expanded my example for the OP.
The circle will be drawn wherever you tell it to. The way that you tell it is by the Location and Size properties of the Rectangle. Try changing the Location that you use. If it is currently at top left then that is 0, 0, try changing that to 10, 10 or something else (remember to keep the numbers to less than the size of your PictureBox.

[Edit - After your comment]
No you didn't say you wanted to draw it with the mouse.

Take a look at this[^] thread. Scroll down till you find the reply from 'dongarbage'. That will give you the basics.
[/Edit]
 
Share this answer
 
v2
Comments
bosco_boom 21-Jan-11 20:21pm    
maybe I didn`t explain myself...I want to draw it using the mouse(with my own hand)...after the image is loaded...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900