I have pictureBox click event. I get coordinates of click and try t draw circle:
private void pictureMain_Click(object sender, EventArgs e){
MouseEventArgs me = (MouseEventArgs)e;
Point coordinates = me.Location;
int x = coordinates.X;
int y = coordinates.Y;
// Create pen.
Pen blackPen = new Pen(Color.Red, 2);
// Create rectangle for ellipse.
Rectangle rect = new Rectangle(x, y, 50, 50);
g.DrawEllipse(blackPen, rect);
}
But it draws circle not in coordinates(x,y) of picturebox. It places circle in another place.
Try this:
private void pictureBox1_Click (object sender, EventArgs e)
{
Point ellipseCenter = ((MouseEventArgs) e).Location;
Size ellipseSize = new Size (50, 50);
Point rectPosition = new Point (ellipseCenter.X - ellipseSize.Width / 2, ellipseCenter.Y - ellipseSize.Height / 2);
Rectangle rect = new Rectangle (rectPosition, ellipseSize);
using (Graphics grp = Graphics.FromImage (pictureBox1.Image))
{
grp.DrawEllipse (Pens.Red, rect);
}
pictureBox1.Refresh ();
}
Related
I need to be able to obtain coordinates and draw a polygon based on the given coordinates.
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
System.Drawing.Graphics g;
g = this.CreateGraphics();
g = Graphics.FromImage(pictureBox1.Image);
Pen p = new Pen(Color.Black, 5);
Point[] a = { new Point(100, 100), new Point(200, 100), new
Point(300, 200), new Point(200, 200), new Point(100, 400) };
g.DrawPolygon(p, a);
g.Dispose();
pictureBox1.Refresh();
This code is for drawing a simple polygon with predefined points, but i need to get the points dynamically from an existing file consisting of the points.
private void button1_Click(object sender, EventArgs e)
{
string line;
List<PointF> polygonData = new List<PointF>();
System.Drawing.Graphics g;
//drawing on picture box
g = Graphics.FromImage(pictureBox1.Image);
Pen p = new Pen(Color.Black, 5);
//a loop to collect all points available in the file
while ((line = file.ReadLine()) != null)
{
var tokens = line.Split(splitChars);
lat = float.Parse(tokens[0].ToString());
longi = float.Parse(tokens[1].ToString());
polygonData.Add(new PointF(lat, longi));
}
if (polygonData.Count > 1)
{
//draw polygon based on points parsed to the loop
g.DrawPolygon(p, polygonData.ToArray());
}
I have drawn a grid on form 8 in on paint event of the form as shown below. I have written a class drawRules in which I mentioned to draw the vertical and horizontal lines based on input from the form.
protected override void OnPaint(PaintEventArgs e)
{
Graphics g;
g = e.Graphics;
Pen linePen = new Pen(System.Drawing.Color.CornflowerBlue);
Int32 Num_of_Lines;
Int32 gridLength;
Int32 gridWidth;
bool IsIntValue = Int32.TryParse(Form7.setValue2, out Num_of_Lines);
bool IsIntValue1 = Int32.TryParse(Form7.setValue3, out gridWidth);
bool IsIntValue2 = Int32.TryParse(Form7.setValue4, out gridLength);
this.Size = new Size(Num_of_Lines * gridWidth, Num_of_Lines * gridLength);
if (IsIntValue)
{
if (IsIntValue1)
{
if (IsIntValue2)
{
drawRules.verticalRule vr1 = new drawRules.verticalRule(g, gridWidth, gridLength, Num_of_Lines);
//Draw horizontal line
drawRules.horizontalRule hr1 = new drawRules.horizontalRule(g, gridWidth, gridLength, Num_of_Lines);
}
linePen.Dispose();
base.OnPaint(e);
}
}
}
after this I want to draw circles wherever mouse is clicked for which I mentioned the mouse click event
private void Form8_MouseClick_1(object sender, MouseEventArgs e)
{
int r1 = e.X;
int r2 = e.Y;
Graphics g2;
g2 = this.CreateGraphics();
drawRules newclass1 = new drawRules();
newclass1.addcoordinate(r1, r2, g2);
}
addcoordinate1 is a method in drawRules class which is called to draw circle. Also, i am writing those coordinates in a file
public void addcoordinate(int r1, int r2, Graphics g2)
{
int gridWidth;
int gridLength;
int Num_of_Lines;
bool IsIntValue = Int32.TryParse(Form7.setValue2, out Num_of_Lines);
bool IsIntValue1 = Int32.TryParse(Form7.setValue3, out gridWidth);
bool IsIntValue2 = Int32.TryParse(Form7.setValue4, out gridLength);
Rectangle rectangle = new Rectangle();
PaintEventArgs arg = new PaintEventArgs(g2, rectangle);
Pen redPen1 = new Pen(Color.Red, 3);
DrawCircle(arg, redPen1, r1, r2, 8, 8);
System.IO.StreamWriter objWriter;
objWriter = new System.IO.StreamWriter("test.txt", true);
objWriter.Write(r1);
objWriter.Write(" ");
objWriter.Write(r2);
objWriter.WriteLine();
objWriter.Close();
}
public void DrawCircle(PaintEventArgs e, Pen redpen1, int x, int y, int
width, int height)
{
e.Graphics.DrawEllipse(redpen1, x - width / 2, y - height / 2, width, height);
redpen1.Dispose();
}
Now, I want to delete that circle, for which mouse is double clicked inside circle's area.
Please suggest how to delete the circles without deleting the grids behind. I will be grateful, if someone help me in this.
Don't draw in the code that gets executed when clicking the Button.
Save the coordinates and let the OnPaint methode handle it.
To remove your cirlces just remove them from the list.
private List<Point> circleCoordinates = new List<Point>();
public Form1()
{
InitializeComponent();
}
public void addcoordinate(int r1, int r2)
{
this.circleCoordinates.Add(new Point(r1, r2));
}
protected override void OnPaint(PaintEventArgs e)
{
// linedrawing goes here
foreach (Point point in this.circleCoordinates)
{
e.Graphics.DrawEllipse(Pens.Black, new Rectangle(point, new Size(10, 10)));
}
base.OnPaint(e);
}
private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
{
for (int i = this.circleCoordinates.Count() - 1; i >= 0; i--)
{
Rectangle ellipseRectangle = new Rectangle(this.circleCoordinates[i].X - 5, this.circleCoordinates[i].Y - 5, 10, 10)
GraphicsPath path = new GraphicsPath();
path.AddEllipse(ellipseRectangle);
if(path.IsVisible(e.Location))
{
this.circleCoordinates.RemoveAt(i);
}
//invalidate form to trigger repainting
this.Invalidate();
}
I already drew ellipse using
g.DrawEllipse(p, 0, 0, 100, 100);
p = new Pen(Color.Red, 1f);
This is my code for moving picturbox
int x, y;
private void timer1_Tick(object sender, EventArgs e)
{
y+=5;
x += 2;
pictureBox3.Location = new Point(x, y);
Invalidate();
}
Picture box move in correctly ...
But I want ...How can i move Picturebox inside that ellipse?
Ellipse should be fixed one..
I used
Pen pen = new Pen(Color.Red);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
to shape the border of rectangle but now I only need to show the corner of that rectangle.
You can draw it by yourself by DrawLine function in Paint event handler, something like this:
Pen pen = new Pen(Color.Red);
private void Form1_Load(object sender, System.EventArgs e)
{
pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
}
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawLine(pen, 0, 0, pictureBox1.Right, 0);
g.DrawLine(pen, 0, 0, 0, pictureBox1.Bottom);
}
It's a use case, maybe you need other coordinates, but you can fix it easily.
You could use 2 lines to get the effect you want:
private void MainForm_Paint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.Red);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
e.Graphics.DrawLine(pen, 0, 0, 50, 0 );
e.Graphics.DrawLine(pen, 0, 0, 0, 50);
}
This draws the corner of a rectangle in the top left corner of the form.
I have this code that draws a Rectangle ( Im trying to remake the MS Paint )
case "Rectangle":
if (tempDraw != null)
{
tempDraw = (Bitmap)snapshot.Clone();
Graphics g = Graphics.FromImage(tempDraw);
Pen myPen = new Pen(foreColor, lineWidth);
g.DrawRectangle(myPen, x1, y1, x2-x1, y2-y1);
myPen.Dispose();
e.Graphics.DrawImageUnscaled(tempDraw, 0, 0);
g.Dispose();
}
But what if I want to draw a circle, what will change?
g.DrawRectangle(myPen, x1, y1, x2-x1, y2-y1);
There is no DrawCircle method; use DrawEllipse instead. I have a static class with handy graphics extension methods. The following ones draw and fill circles. They are wrappers around DrawEllipse and FillEllipse:
public static class GraphicsExtensions
{
public static void DrawCircle(this Graphics g, Pen pen,
float centerX, float centerY, float radius)
{
g.DrawEllipse(pen, centerX - radius, centerY - radius,
radius + radius, radius + radius);
}
public static void FillCircle(this Graphics g, Brush brush,
float centerX, float centerY, float radius)
{
g.FillEllipse(brush, centerX - radius, centerY - radius,
radius + radius, radius + radius);
}
}
You can call them like this:
g.FillCircle(myBrush, centerX, centerY, radius);
g.DrawCircle(myPen, centerX, centerY, radius);
Try the DrawEllipse method instead.
You'll need to use DrawEllipse if you want to draw a circle using GDI+.
An example is here: http://www.websupergoo.com/helpig6net/source/3-examples/9-drawgdi.htm
You should use DrawEllipse:
//
// Summary:
// Draws an ellipse defined by a bounding rectangle specified by coordinates
// for the upper-left corner of the rectangle, a height, and a width.
//
// Parameters:
// pen:
// System.Drawing.Pen that determines the color, width,
// and style of the ellipse.
//
// x:
// The x-coordinate of the upper-left corner of the bounding rectangle that
// defines the ellipse.
//
// y:
// The y-coordinate of the upper-left corner of the bounding rectangle that
// defines the ellipse.
//
// width:
// Width of the bounding rectangle that defines the ellipse.
//
// height:
// Height of the bounding rectangle that defines the ellipse.
//
// Exceptions:
// System.ArgumentNullException:
// pen is null.
public void DrawEllipse(Pen pen, int x, int y, int width, int height);
if you want to draw circle on button then this code might be use full.
else if you want to draw a circle on other control just change the name of control and also event. like here event button is called. if you want to draw this circle in group box call the Groupbox event.
regards
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.button1.Location = new Point(108, 12);
// this.Paint += new PaintEventHandler(Form1_Paint);
this.button1.Paint += new PaintEventHandler(button1_Paint);
}
void button1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.button1.CreateGraphics();
Pen pen = new Pen(Color.Red);
g.DrawEllipse(pen, 10, 10, 20, 20);
}
}
With this code you can easily draw a circle...
C# is great and easy my friend
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Graphics myGraphics = base.CreateGraphics();
Pen myPen = new Pen(Color.Red);
SolidBrush mySolidBrush = new SolidBrush(Color.Red);
myGraphics.DrawEllipse(myPen, 50, 50, 150, 150);
}
}
private void DrawEllipseRectangle(PaintEventArgs e)
{
Pen p = new Pen(Color.Black, 3);
Rectangle r = new Rectangle(100, 100, 100, 100);
e.Graphics.DrawEllipse(p, r);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
DrawEllipseRectangle(e);
}
PictureBox circle = new PictureBox();
circle.Paint += new PaintEventHandler(circle_Paint);
void circle_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawEllipse(Pens.Red, 0, 0, 30, 30);
}