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.
luci margineanPosted Jan 22, 2011, 4:37 PM
FroglegPosted Jan 22, 2011, 2:13 PM
public partial class Form1 : Form
{
bool _mouseDown = false;
Point origPt, newPt,centerPt;
int rectWidth, rectHeight;
Rectangle ellipseRect,clearRect;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void pcbImage_Paint(object sender, PaintEventArgs e)
{
if (ellipseRect !=null)
{
e.Graphics.DrawEllipse(Pens.Red, ellipseRect);
}
}
private void pcbImage_MouseDown(object sender, MouseEventArgs e)
{
_mouseDown = true;
centerPt.X = e.X;
centerPt.Y = e.Y;
}
private void pcbImage_MouseMove(object sender, MouseEventArgs e)
{
if (_mouseDown == true)
{
newPt.X = e.X;
newPt.Y = e.Y;
Graphics g = pcbImage.CreateGraphics(); // draw temp ellipse
clearRect = Rectangle.Inflate(ellipseRect, 5, 5); //erase old ellipse
Region rg = new Region(clearRect); //erase old ellipse
pcbImage.Invalidate(rg); //erase old ellipse
rectWidth = Math.Abs(centerPt.X - newPt.X);// calculate half width of ellipse
rectHeight = Math.Abs(centerPt.Y - newPt.Y);// calculate half height of ellipse
origPt.X = centerPt.X - rectWidth;
origPt.Y = centerPt.Y - rectHeight;
ellipseRect = new Rectangle(origPt.X, origPt.Y, (rectWidth*2),(rectHeight *2));
g.DrawEllipse(Pens.Red, ellipseRect); // draw temp ellipse
}
}
private void pcbImage_MouseUp(object sender, MouseEventArgs e)
{
_mouseDown = false;
}
}
luci margineanPosted Jan 22, 2011, 11:45 AM
e.Graphics.DrawPath
This is the code what does the drawing, I also have a button(btnLoad) and a pictureBox(pcbImage) where the image is loaded
Code:
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);
}
}
}
FroglegPosted Jan 22, 2011, 2:34 AM
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawEllipse(Pens.Red, 10, 10, 100, 100);
// e.Graphics.DrawEllipse(Pens.Red, X, Y, width, height);
}
draws a circle 10 pixels across and 10 pixels down from top left corner of picturebox, with a diameter of 100 pixels