System.Drawing.Graphics.DrawRectangle C# (CSharp) Method

DrawRectangle() public method

public DrawRectangle ( Pen pen, Rectangle rect ) : void
pen Pen
rect Rectangle
return void
        public void DrawRectangle(Pen pen, Rectangle rect)
        {
            if (pen == null)
                throw new ArgumentNullException ("pen");

            RectanglePath (new RectangleF(rect.X, rect.Y, rect.Width, rect.Height));
            StrokePen (pen);
        }

Same methods

Graphics::DrawRectangle ( Pen pen, float x1, float y1, float x2, float y2 ) : void
Graphics::DrawRectangle ( Pen pen, int x1, int y1, int x2, int y2 ) : void

Usage Example

        public override void Draw(System.Drawing.RectangleF dirtyRect)
        {
            var g = new Graphics();

            // NSView does not have a background color so we just use Clear to white here
            g.Clear(Color.White);

            //RectangleF ClientRectangle = this.Bounds;
            RectangleF ClientRectangle = dirtyRect;

            // Calculate the location and size of the drawing area
            // within which we want to draw the graphics:
            Rectangle rect = new Rectangle((int)ClientRectangle.X, (int)ClientRectangle.Y,
                                           (int)ClientRectangle.Width, (int)ClientRectangle.Height);
            drawingRectangle = new Rectangle(rect.Location, rect.Size);
            drawingRectangle.Inflate(-offset, -offset);
            //Draw ClientRectangle and drawingRectangle using Pen:
            g.DrawRectangle(Pens.Red, rect);
            g.DrawRectangle(Pens.Black, drawingRectangle);
            // Draw a line from point (3,2) to Point (6, 7)
            // using the Pen with a width of 3 pixels:
            Pen aPen = new Pen(Color.Green, 3);
            g.DrawLine(aPen, Point2D(new PointF(3, 2)),
                       Point2D(new PointF(6, 7)));

            g.PageUnit = GraphicsUnit.Inch;
            ClientRectangle = new RectangleF(0.5f,0.5f, 1.5f, 1.5f);
            aPen.Width = 1 / g.DpiX;
            g.DrawRectangle(aPen, ClientRectangle);

            aPen.Dispose();

            g.Dispose();
        }
All Usage Examples Of System.Drawing.Graphics::DrawRectangle