使用System.Drawing绘制圆圈

时间:2009-12-02 18:59:44

标签: c# drawing

我有这个代码绘制一个Rectangle(我试图重新制作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();
                }

但是,如果我想绘制一个圆圈,会发生什么变化呢?

g.DrawRectangle(myPen, x1, y1, x2-x1, y2-y1);

8 个答案:

答案 0 :(得分:29)

没有DrawCircle方法;请改用DrawEllipse。我有一个静态类,其中包含绘制和填充圆圈的图形扩展方法(此处未显示)。它们是DrawEllipseFillEllipse

的包装
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);
    }
}

您可以这样称呼它们:

g.FillCircle(myBrush, center X, centerY, radius);
g.DrawCircle(myPen, centerX, centerY, radius);

答案 1 :(得分:20)

请尝试使用DrawEllipse方法。

答案 2 :(得分:6)

如果您想使用GDI +绘制圆圈,则需要使用DrawEllipse

这里有一个例子:http://www.websupergoo.com/helpig6net/source/3-examples/9-drawgdi.htm

答案 3 :(得分:4)

您应该使用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);

答案 4 :(得分:2)

  

如果您想在按钮上绘制圆圈,则此代码可能已使用完整。   否则,如果你想在其他控件上绘制一个圆圈,只需更改控件名称和事件。像这里调用事件按钮。如果要在组框中绘制此圆圈,请调用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);

    }





}

答案 5 :(得分:1)

使用此代码,您可以轻松绘制圆圈... 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);
    }
 }

答案 6 :(得分:0)

     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);
        }

答案 7 :(得分:0)

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);
        }