【问题标题】:Drawing circles with System.Drawing使用 System.Drawing 绘制圆圈
【发布时间】:2020-04-04 00:01:55
【问题描述】:

我有这段代码可以绘制一个矩形(我正在尝试重新制作 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);

【问题讨论】:

    标签: c# drawing


    【解决方案1】:

    没有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, centerX, centerY, radius);
    g.DrawCircle(myPen, centerX, centerY, radius);
    

    【讨论】:

    • 我喜欢这个,因为它是可重用的代码。好东西!
    【解决方案2】:

    改用DrawEllipse 方法。

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案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);
        

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

            使用此代码,您可以轻松地画一个圆... 我的朋友,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);
                }
             }
            

            【讨论】:

            • 请定义“基础”。
            【解决方案7】:
                 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);
                    }
            

            【讨论】:

              【解决方案8】:
              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);
                      }
              

              【讨论】:

              • 请解释您的答案提供了其他答案所缺乏的内容。
              • 简洁明了...与其他人相比。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2014-02-20
              • 1970-01-01
              • 1970-01-01
              • 2021-10-15
              • 2015-01-31
              • 2014-04-11
              • 2012-09-19
              相关资源
              最近更新 更多