So I have a code that needs to draw a circle, on random position, and also it needs to stay in predefined borders. In my code, I'm supposed to draw only one circle. In some cases, it draws only one but in other cases, it draws two of them and I have no idea why.
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Random r = new Random();
int leftX = 20;
int topY = 60;
int width = this.Width - (3 * leftX);
int height = this.Height - (int)(2.5 * topY);
float x = r.Next(20, width - 100);
float y = r.Next(60, height - 100);
Rectangle rect = new Rectangle((int)x, (int)y, 100, 100);
g.DrawRectangle(new Pen(Color.Black), rect); //rectangle around ellipse
g.DrawRectangle(new Pen(Color.Black, 3), leftX, topY, width, height); //border rectangle
g.FillEllipse(new SolidBrush(Color.Black), rect);
}
The paint event gets called whenever the form is re-drawn, so from your point of view, that's unpredictable. You would be better moving the code to generate the position into the load event:
float x;
float y;
private void Form1_Load(object sender, System.EventArgs e)
{
Random r = new Random();
x = r.Next(20, width - 100);
y = r.Next(60, height - 100);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
int leftX = 20;
int topY = 60;
int width = this.Width - (3 * leftX);
int height = this.Height - (int)(2.5 * topY);
Rectangle rect = new Rectangle((int)x, (int)y, 100, 100);
g.DrawRectangle(new Pen(Color.Black), rect); //rectangle around ellipse
g.DrawRectangle(new Pen(Color.Black, 3), leftX, topY, width, height); //border rectangle
g.FillEllipse(new SolidBrush(Color.Black), rect);
}
Related
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 ();
}
I want to draw curved area with color(black in my case) in a rectangle.
I tried multiple things FillPie, FillEllipse in OnPaint event but not able to do, I want to draw like thisFill Curved Area
I have tried the below code. but this is not what I want
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics gr = e.Graphics;
int x = 50;
int y = 50;
int width = 100;
int height = 100;
Rectangle rect = new Rectangle(x, y, width / 2, height / 2);
gr.FillRectangle(Brushes.Black, rect);
gr.FillPie(Brushes.White, x, y, width, height, 180, 90);
using (Pen pen = new Pen(Color.Yellow, 1))
gr.DrawArc(pen, x, y, width, height, 180, 90);
}
this code is drawing like this. I dont want to create extra rectangle. MyCode
I'm not 100% sure if this is that what you wanted:
I achieved this by creating a Region with a quarter of the specified rectangle. I then exclude the pie from it using a GraphicsPath. The resulting curve is then filled using Graphics.FillRegion with a black brush:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics gr = e.Graphics;
int x = 50;
int y = 50;
int width = 100;
int height = 100;
Rectangle rect = new Rectangle(x, y, width/ 2, height / 2);
Region r = new Region(rect);
GraphicsPath path = new GraphicsPath();
path.AddPie(x, y, width, height, 180, 90);
r.Exclude(path);
gr.FillRegion(Brushes.Black,r);
}
I get a JIT compiling error running this code
void draw(PaintEventArgs e)
{
Graphics gr =this.CreateGraphics();
Pen pen = new Pen(Color.Black, 5);
int x = 50;
int y = 50;
int width = 100;
int height = 100;
gr.DrawEllipse(pen, x, y, width, height);
gr.Dispose();
SolidBrush brush = new SolidBrush(Color.White);
gr.FillEllipse(brush, x,y,width,height);
}
Error says: System Argument Exception: Invalid argument in
FillEllipse(Brush, int32 x,int32 y,int32 width,int 32 height);
Since you are passing in the PaintEventArgs e you can and should use its e.Graphics!
And since you didn't create it, don't dispose of it!
But those Pens and Brushes you create you should disposed of or better yet, create them inside a using clause! For the SolidBrush we can use the standard Brush, which we can't change and must not dispose of either!
To be sure the Fill won't overwrite the Draw I have switched the order.
So, try this:
void draw(PaintEventArgs e)
{
Graphics gr = e.Graphics;
int x = 50;
int y = 50;
int width = 100;
int height = 100;
gr.FillEllipse(Brushes.White, x, y, width, height);
using (Pen pen = new Pen(Color.Black, 5) )
gr.DrawEllipse(pen, x, y, width, height);
}
I'm drawing a rectangle with the mouse and now i want that while i'm drawing the rectangle it will draw points on each of the rectangle edges bottom,top,keft,right.
This is how it look like when i'm drawing just the rectangle:
And i want that while i'm drawing the rectangle in real time to add/pad each edge of the rectangle with X number of points. For example on each edge 10 green points with exactly space between them.
For example i added the points in paint just to show what i mean:
Just the green points should be in the thick of the red lines of the rectangle and on the red lines.
The green points should be filled.
And there should be exact space between the points.
I just drawed some points on the top but it should be on the left bottom and right too.
This is how i draw now the regular rectangle just the rectangle.
In the top of form1:
private Bitmap _bmpBU = null;
public static Rectangle mRect;
In the constructor:
this.DoubleBuffered = true;
_bmpBU = new Bitmap(#"D:\MyWeatherStation-Images-And-Icons\radar090.PNG");
pictureBox1 mouse move event:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mRect = new Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top);
pictureBox1.Invalidate();
}
}
pictureBox1 mouse down event:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
mRect = new Rectangle(e.X, e.Y, 0, 0);
Image iOLd = this.pictureBox1.Image;
Bitmap bmp = (Bitmap)_bmpBU.Clone();
this.pictureBox1.Image = bmp;
if (iOLd != null)
iOLd.Dispose();
pictureBox1.Invalidate();
}
And the pictureBox1 paint event:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, mRect);
}
}
I need that somehow in the paint event while it's drawing the rectangle it will also pad and add the green points to each edge.
This is just math. Add this function:
private void DrawPointsOnRectangle(Graphics g, int numberOfPoints)
{
var brush = new SolidBrush(Color.DarkGreen);
const int rectanglePenWidth = 2;
//North & South
int spacing = mRect.Width / (numberOfPoints - 1);
for (int x = 0; x < numberOfPoints; x++)
{
g.FillEllipse(brush, mRect.X + (x * spacing) - rectanglePenWidth - 5, mRect.Y - 7, 15, 15);
g.FillEllipse(brush, mRect.X + (x * spacing) - rectanglePenWidth - 5, mRect.Y - 7 + mRect.Height, 15, 15);
}
//East & West
spacing = mRect.Height/(numberOfPoints - 1);
for (int y = 0; y < numberOfPoints; y++)
{
g.FillEllipse(brush, mRect.X - rectanglePenWidth - 5, mRect.Y - 7 + (y * spacing), 15, 15);
g.FillEllipse(brush, mRect.X - rectanglePenWidth - 5 + mRect.Width, mRect.Y - 7 + (y * spacing), 15, 15);
}
}
And modify your pictureBox1_Paint function to call the new function:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, mRect);
DrawPointsOnRectangle(e.Graphics, 5);
}
}
That should do it! You can change the 5 parameter to however many points you want on each side.
private Point RectStartPoint;
private Image img;
private Image imgClone;
private Pen myPen;
private n = 10; //number of points
You have to initialize those objects, maybe in:
public Form1()
{
InitializeComponent();
myPen = new Pen(Brushes.Red, 2);
//Bitmap to hold the picturebox image
img = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g;
using (g = Graphics.FromImage(img))
{
g.DrawImage(imageOfPicturebox, 0, 0, pictureBox1.Width, pictureBox1.Height);
}
//image to hold the original picturebox. We need it to clear img to the original
//picturebox image
imgClone = (Bitmap)img.Clone();
//We draw always on img and then we invalidate
pictureBox1.Image = img;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
RectStartPoint = e.Location;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && e.Location != RectStartPoint)
{
DrawRectangle(e.Location);
}
}
private void DrawRectangle(Point pnt)
{
Graphics g = Graphics.FromImage(img);
int width, height, i, x, y;
g.SmoothingMode = SmoothingMode.AntiAlias;
//Clear img from the rectangle we drawn previously
g.DrawImage(imgClone, 0, 0);
if (pnt.X == RectStartPoint.X || pnt.Y == RectStartPoint.Y)
{
g.DrawLine(myPen, RectStartPoint.X, RectStartPoint.Y, pnt.X, pnt.Y);
}
else
{
g.DrawRectangle(myPen, Math.Min(RectStartPoint.X, pnt.X), Math.Min(RectStartPoint.Y, pnt.Y),
Math.Abs(RectStartPoint.X - pnt.X), Math.Abs(RectStartPoint.Y - pnt.Y));
//width of spaces between points
width = (int)((Math.Abs(RectStartPoint.X - pnt.X)) / (n - 1));
//height of spaces between points
height = (int)((Math.Abs(RectStartPoint.Y - pnt.Y)) / (n - 1));
//we always want the upper left x, y coordinates as a reference drawing clockwise
x = Math.Min(RectStartPoint.X, pnt.X);
y = Math.Min(RectStartPoint.Y, pnt.Y);
//Drawing the points. change the 3, 6 values for larger ones
for (i = 0; i < n - 1; i++)
{
//Up side
g.FillEllipse(Brushes.Green, new Rectangle(x - 3, Math.Min(RectStartPoint.Y, pnt.Y) - 3, 6, 6));
//Right side
g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) + Math.Abs(RectStartPoint.X - pnt.X) - 3, y - 3, 6, 6));
//Bottom side
g.FillEllipse(Brushes.Green, new Rectangle(x - 3, Math.Min(RectStartPoint.Y, pnt.Y) + Math.Abs(RectStartPoint.Y - pnt.Y) - 3, 6, 6));
//Left side
g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) - 3, y - 3, 6, 6));
x += width;
y += height;
}
g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) + Math.Abs(RectStartPoint.X - pnt.X) - 3,
Math.Min(RectStartPoint.Y, pnt.Y) - 3, 6, 6));
g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) + Math.Abs(RectStartPoint.X - pnt.X) - 3,
Math.Min(RectStartPoint.Y, pnt.Y) + Math.Abs(RectStartPoint.Y - pnt.Y) - 3, 6, 6));
}
g.Dispose();
//draw img to picturebox
pictureBox1.Invalidate();
}
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);
}