使用System.Drawing绘制圆圈

Ton*_*ony 18 c# drawing

我有这个代码绘制一个矩形(我试图重新制作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();
                }
Run Code Online (Sandbox Code Playgroud)

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

g.DrawRectangle(myPen, x1, y1, x2-x1, y2-y1);
Run Code Online (Sandbox Code Playgroud)

Oli*_*bes 32

没有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);
    }
}
Run Code Online (Sandbox Code Playgroud)

你可以像这样打电话给他们:

g.FillCircle(myBrush, center X, centerY, radius);
g.DrawCircle(myPen, centerX, centerY, radius);
Run Code Online (Sandbox Code Playgroud)

  • 我喜欢这个,因为它是可重用的代码.好东西! (3认同)

Ste*_*ton 21

请尝试使用DrawEllipse方法.


Mic*_*odd 6

如果要使用GDI +绘制圆,则需要使用DrawEllipse.

这方面的一个例子是:http://www.websupergoo.com/helpig6net/source/3-examples/9-drawgdi.htm