【问题标题】:Graphics.DrawRectangle(Pen, RectangleF)Graphics.DrawRectangle(Pen, RectangleF)
【发布时间】:2009-01-20 20:37:37
【问题描述】:

http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawrectangle.aspx

FillRectangle、DrawRectangle、FillElipse 和 DrawEllipse 都可以采用 4 个 Float(或“Single”)参数:x、y、width、height。不过,DrawRectangle 是唯一不采用 RectangleF 的。

我想知道是否有人知道这是为什么。看来他们只是忘记了超载。

【问题讨论】:

    标签: .net graphics drawing overloading rectangles


    【解决方案1】:

    嗯,这对我来说确实也像是一个遗漏。

    有趣的是,有一个 DrawRectangles 的重载,它接受一个 RectangleF[] 数组作为参数。

    所以我想如果需要,你可以将它与一个数组大小一起使用。

    【讨论】:

      【解决方案2】:

      根据安迪的回答,扩展名应该如下

      public static class GraphicsExtensions
      {
          public static void DrawRectangle(this Graphics g, Pen pen, RectangleF rect)
          {
              g.DrawRectangles(pen, new[] { rect });
          }
      }
      

      【讨论】:

        【解决方案3】:

        根据安迪的回答,这种简单的扩展方法让生活更轻松。

        using System.Drawing;
        
        public static class GraphicsExtensions
        {
            public static void DrawRectangle(this Graphics g, Pen pen, RectangleF rect) =>
                g.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height);
        }
        

        【讨论】:

          【解决方案4】:

          我知道这个问题很老,但仅供参考:我认为正确的方法是使用roundtruncate,例如:

          Dim BBox As RectangleF = ListOfRectangleF(3)         ' get RectangleF any way you have it
          Dim p As New Pen(Brushes.DarkRed)
          e.Graphics.DrawRectangle(p, Rectangle.Round(ptBBox)) ' draw RectangleF using Rectangle.Round()
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-03-28
            • 1970-01-01
            • 2016-04-01
            • 2014-12-22
            • 2010-11-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多