C# 使用 System.Drawing 绘制圆
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1835062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawing circles with System.Drawing
提问by Tony
I have this code that draws a Rectangle ( Im trying to remake the MS Paint )
我有这个绘制矩形的代码(我正在尝试重新制作 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);
采纳答案by Stephen Wrighton
Try the DrawEllipsemethod instead.
请尝试使用DrawEllipse方法。
回答by Michael Todd
You'll need to use DrawEllipseif you want to draw a circle using GDI+.
如果要使用 GDI+ 绘制圆,则需要使用DrawEllipse。
An example is here: http://www.websupergoo.com/helpig6net/source/3-examples/9-drawgdi.htm
一个例子在这里:http: //www.websupergoo.com/helpig6net/source/3-examples/9-drawgdi.htm
回答by Rubens Farias
You should use DrawEllipse
:
你应该使用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);
回答by Malik Dilawar
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
如果你想在按钮上画圆圈,那么这段代码可能会被完整使用。否则如果你想在其他控件上画一个圆圈,只需更改控件的名称和事件。就像这里的事件按钮被调用。如果要在分组框中绘制此圆圈,请调用 Groupbox 事件。问候
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);
}
}
回答by Olivier Jacot-Descombes
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
:
没有DrawCircle
方法;使用DrawEllipse
来代替。我有一个带有方便的图形扩展方法的静态类。以下是绘制和填充圆圈。它们是围绕DrawEllipse
和的包装器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);
回答by MOKp
With this code you can easily draw a circle... C# is great and easy my friend
使用此代码,您可以轻松绘制圆圈... C# 很棒而且很容易我的朋友
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);
}
}
回答by Bokooo
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);
}
回答by Adekunle Owolabi
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);
}