Graphics.DrawRectangle (Penna, Rettangolo F)
-
19-08-2019 - |
Domanda
http://msdn.microsoft.com/ it-it / library / system.drawing.graphics.drawrectangle.aspx
FillRectangle, DrawRectangle, FillElipse e DrawEllipse possono accettare tutti 4 parametri Float (o "Single"): x, y, larghezza, altezza. DrawRectangle è l'unico che non prenderà un RectangleF, tuttavia.
Mi chiedevo se qualcuno sapesse perché. Sembra che abbiano semplicemente dimenticato di sovraccaricarlo.
Soluzione
Beh, sicuramente mi sembra un'omissione.
È interessante notare che c'è un sovraccarico di DrawRectangles che accetta un array RectangleF [] come parametro.
Quindi suppongo che potresti usarlo con una dimensione dell'array di uno se necessario.
Altri suggerimenti
Secondo la risposta di Andy l'estensione dovrebbe essere come di seguito
public static class GraphicsExtensions
{
public static void DrawRectangle(this Graphics g, Pen pen, RectangleF rect)
{
g.DrawRectangles(pen, new[] { rect });
}
}
In seguito alla risposta di Andy, questo semplice metodo di estensione semplifica la vita.
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);
}
So che questa domanda è vecchia, ma solo per riferimento: credo che il modo corretto sia usare round o troncare , come ad esempio:
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()