Draws a rectangle.


void DrawRectangle(Pen pen, double x, double y, double width, double height);
void DrawRectangle(Pen pen, RectangleF rect);

[Visual Basic]

Sub DrawRectangle(pen As Pen, x As Double, y As Double, width As Double, height As Double)
Sub DrawRectangle(pen As Pen, rect As RectangleF)
Name Description
pen The pen to use for drawing the rectangle.
x The left coordinate of the rectangle.
y The top coordinate of the rectangle.
width The width of the rectangle.
height The height of the rectangle.
rect The rectangle to be drawn.



The following code draws an outline of a rectangle, then paints another rectangle within it and finally paints and outlines another rectangle within that.


using (Bitmap bm = new Bitmap(180, 180)) {
  using (Graphics gr = Graphics.FromImage(bm)) {
    gr.DrawRectangle(new Pen(Color.Black), new RectangleF(20, 20, 160, 160));
    gr.FillRectangle(Brushes.Plum, new RectangleF(40, 40, 120, 120));
    gr.DrawRectangle(new Pen(Color.Red), new RectangleF(60, 60, 80, 80));
    gr.FillRectangle(Brushes.DarkOrchid, new RectangleF(60, 60, 80, 80));