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);
}
Related
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);
}
I have a Label and I am trying to draw an inner circle (not filled) inside this Label, as big as possible.
I have tried two methods, one method applied to label1 and another one applied to label2. In both cases it does not work.
Note: the Label should keep its background color and content.
How can I get rid of this?
Code:
void DrawCircle1(Graphics g, Point centerPos, int radius, int cutOutLen)
{
RectangleF rectangle = new RectangleF(centerPos.X, centerPos.Y,
radius * 2,
radius * 2
);
// calculate the start angle
float startAngle = (float)(Math.Asin(
1f * (radius - cutOutLen) / radius) / Math.PI * 180);
using (System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath())
{
path.AddArc(rectangle, 180 - startAngle, 180 + 2 * startAngle);
path.CloseFigure();
//g.FillPath(Brushes.Yellow, path);
using (Pen p = new Pen(Brushes.Yellow))
{
g.DrawPath(new Pen(Brushes.Blue, 2), path);
}
}
}
private void DrawCircle2(PaintEventArgs e)
{
Label tempLabel = label2;
using (System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red))
{
using (System.Drawing.Pen myPen = new Pen(myBrush, 2))
{
e.Graphics.DrawEllipse(myPen, new System.Drawing.Rectangle(tempLabel.Location.X, tempLabel.Location.Y,
tempLabel.Width, tempLabel.Height));
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
DrawCircle1(e.Graphics, new Point(label1.Width/2, label1.Height/2), 10, 50);
DrawCircle2(e);
}
Below a screenshot:
You are drawing on Form not the Label. Instead of overriding OnPaint method of the Form, try to handle Paint event of Label controls. For example:
private void label1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.DrawEllipse(Pens.Red, 0, 0, label1.Height - 1, label1.Height - 1);
}
Same procedures you're using now, calling them from a control's Paint() event.
It's the same if you're creating a Custom control. Use the overridden OnPaint() event in this case.
In the control's Paint() event, call one/more methods to draw a shape on the control's surface.
private void label1_Paint(object sender, PaintEventArgs e)
{
DrawCircle1(e.Graphics, label1.ClientRectangle);
}
private void label2_Paint(object sender, PaintEventArgs e)
{
DrawCircle2(e.Graphics, label2.ClientRectangle);
}
Use the Control's ClientRectangle bounds to derive the size of the figure.
Here, the ClientRectangle is reduced by 1 when using Graphics.DrawEllipse() and by 2 when using Graphics.DrawPath(). The two methods calculate the pen size in relation to the drawing area in a slightly different manner.
private void DrawCircle1(Graphics g, RectangleF canvas)
{
canvas.Inflate(-2, -2);
g.SmoothingMode = SmoothingMode.AntiAlias;
using (GraphicsPath path = new GraphicsPath())
using (Pen p = new Pen(Color.Blue, 2)) {
path.StartFigure();
path.AddArc(canvas, 0, 360);
path.CloseFigure();
g.DrawPath(p, path);
}
}
private void DrawCircle2(Graphics g, RectangleF canvas)
{
canvas.Inflate(-1, -1);
g.SmoothingMode = SmoothingMode.AntiAlias;
using (Pen p = new Pen(Color.Red, 2)) {
g.DrawEllipse(p, canvas);
}
}
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 wrote this code:
private void ResizeImage()
{
SetImage(Image);
}
private void SetPen()
{
CreatePen(10,10,20,60);
}
private void CreatePen(int x, int y, int width, int height)
{
Rectangle = new Rectangle(x, y, width, height);
Pen = new Pen(Color.Crimson, 1);
(Image.CreateGraphics()).DrawRectangle(Pen, Rectangle);
Invalidate();
}
The problem is that the method Load() replaces image to rectangle. I'm doing image Cropper, where the user can not create new selection. The program creates the selection itself, the user can only move it.
In Paint event of PictureBox draw your rectangle:
void PictureBox_Paint(object sender, PaintEventArgs e)
{
Rectangle = new Rectangle(x, y, width, height);
Pen = new Pen(Color.Crimson, 1);
e.Graphics.DrawRectangle(Pen, Rectangle);
}