Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

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

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top