Mike
Mike

Reputation: 2260

Graphics.DrawRectangle(Pen, RectangleF)

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

FillRectangle, DrawRectangle, FillElipse and DrawEllipse all can take 4 Float (or "Single") parameters: x, y, width, height. DrawRectangle is the only one that will not take a RectangleF, though.

I was wondering if anyone knew why this is. It sure seems like they just plain forgot to overload it.

Upvotes: 10

Views: 8864

Answers (4)

Oak_3260548
Oak_3260548

Reputation: 2000

I know this question is old, but just for a reference: I believe the correct way is to use either round or truncate, such as:

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()

Upvotes: 1

  Tourmalin
Tourmalin

Reputation: 13

According to the Andy's answer the extension should be as below

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

Upvotes: 1

Hand-E-Food
Hand-E-Food

Reputation: 12794

Following on from Andy's answer, this simple extension method makes life easier.

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

Upvotes: 0

Andy
Andy

Reputation: 5268

Well it sure does look like an omission to me too.

Interestingly, there is an overload of DrawRectangles that takes a RectangleF[] array as a parameter.

So I suppose you could use this with an array size of one if needed.

Upvotes: 15

Related Questions