Drawing Rectangles in GDI+

In this article I will explain you how to draw rectangles in GDI+.


This article has been excerpted from book "Graphics Programming with GDI+ ".

The basic drawing object is a rectangle. When you draw a rectangle through your applications, you need to specify only the starting point, height and width of the rectangle. GDI+ takes care of the rest.

The Graphics class provides the DrawRectangle method, which draws a rectangle specified by a starting point, a width, and a height. The Graphics class also provides the DrawRectangles method, which draws a series of rectangles specified by an array of Rectangle structures.

DrawRectangle has three overloaded methods. An application can use a Rectangle structure or coordinates of integer or float types to draw a rectangle:

public
void DrawRectangle (Pen, Rectangle);
public
void DrawRectangle (Pen, int, int, int, int);
public
void DrawRectangle (Pen, float, float, float, float);

To draw a rectangle, an application first creates a pen and a rectangle (location, width, and height), and then it calls DrawRectangle. Listing 3.3 draws rectangles using the different overloaded forms of DrawRectangle.

LISTING 3.3: Using DrawRectangle to draw rectangles

private
void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
   
// Create pens and points
    Pen redPen = new Pen(Color.Red, 1);
    
Pen bluePen = new Pen(Color.Blue, 2);
    
Pen greenPen = new Pen(Color.Green, 3);
    
float x = 5.0F, y = 5.0F;
    
float width = 100.0F;
    
float height = 200.0F;
    
// Create a rectangle
    Rectangle rect = new Rectangle(20, 20, 80, 40);
    
// Draw rectangles
    e.Graphics.DrawRectangle(bluePen, x, y, width, height);
    e.Graphics.DrawRectangle(redPen, 60, 80, 140, 50);
    e.Graphics.DrawRectangle(greenPen, rect);
    
// Dispose of objects
    redPen.Dispose();
    bluePen.Dispose();
    greenPen.Dispose();
}

Figure 3.3 shows the output from the Listing 3.3

fig3.3.jpg

FIGURE 3.3: Drawing individual rectangles

The DrawRectangles method draws a series of rectangles using a single pen. It is useful when you need to draw multiple rectangles using the same pen (if you need to draw multiple rectangles using different pens, you must use multiple calls to DrawRectangle). A single call to DrawRectangles is faster than multiple DrawRectangle calls. DrawRectangles takes two parameters- a pen and an array of Rectangle and RectangleF structures- as shown in Listing 3.4.

LISTING 3.4: Using DrawRectangle to draw a series of rectangles


Pen greenPen = new Pen (Color.Green, 4);
RectangleF[] rectArray =
{
        new RectangleF (5.0F, 5.0F, 100.0F, 200.0F),
        new RectangleF (20.0F, 20.0F, 80.0F, 40.0F),
        new RectangleF (60.0F, 80.0F, 140.0F, 50.0F),
};
e.Graphics.DrawRectangles (greenPen, rectArray);
greenPen.Dispose();

Figure 3.4 shows the output from Listing 3.4. As you can see, it's easy to draw multiple rectangles using the DrawRectangles methods.

fig3.4.jpg

FIGURE 3.4: Drawing a series of rectangles

Conclusion

Hope the article would have helped you in understanding drawing rectangles. Read other articles on GDI+ on the website.


bookGDI.jpg This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.