Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

The Basics of GDI+

GDI+ is .NET's way to render low-level graphics. GDI+ uses drawing "primitives," such as lines, curves, circles, and rectangles. In addition, GDI+ supports image manipulation as well as text rendering.


advertisement

n graphical user interfaces such as Microsoft Windows, drawing on the screen is an important task. Everything displayed on the screen is based on simple drawing operations. Often, environments such as Visual Basic abstract those drawing operations away from the developer. However, the same drawing operations still take place under the hood. In Visual Studio .NET, developers have easy access to that drawing functionality whenever they need it through a technology called GDI+. Using GDI+, developers can easily perform drawing operations such as generating graphs or building custom controls.

Whenever you create a Windows Forms (WinForms) application .NET uses (and includes) GDI+ by default. The System.Drawing namespace contains all GDI+ functionality (as well as a few sub-namespaces). GDI+ provides all the basic drawing features, such as drawing lines, curves, circles, ellipses, strings, bitmaps, and more. GDI+ also gives developers the ability to fill areas with colors, patterns, and textures. This article will explain some of the basic drawing mechanisms that GDI+ provides.

The Graphics Object
All GDI+ drawing is done using a Graphics object. This object has a large number of methods that encapsulate drawing operations on a drawing "canvas." GDI+ supports a number of drawing canvases including: windows, bitmaps, and printer pages. Depending on the type of drawing canvas, the Graphics object is created or retrieved in a number of different ways. If you want to modify the contents of a bitmap file you would create a Bitmap canvas using the FromImage() method. This article will focus on one type of canvas: the window.

Drawing in a WinForms form is a somewhat peculiar task, because it is not enough to draw the graphic once. Instead, the "drawing" may have to be "refreshed" (re-drawn) whenever a window requires repainting. Repainting can happen whenever a user resizes a window or one window overlaps another window and then something brings the overlapped windows to the front.

So how do you know when to start drawing? The easiest way is to listen to the Paint event that is fired by every window (or control for that matter). The Paint event is a very good choice for drawing-logic because it fires whenever you need to refresh the display, and because part of the Paint event arguments (parameters) is a handle to the Graphics object. You can access the Graphics object via the following C# code:

   private void Form1_Paint(object sender,
   System.Windows.Forms.PaintEventArgs e)
   {
      Graphics g = e.Graphics;
   }

Visual Basic .NET supports the same construct:

   Private Sub Form1_Paint( _  
      ByVal sender As Object, _  
      ByVal e As System.Windows.Forms.PaintEventArgs)  _
      Handles MyBase.Paint
      Dim g As Graphics = e.Graphics
   End Sub

Once you have a Graphics object, you are ready to draw on the window.

Author's Note: Whenever I refer to a "window" as the drawing canvas, I also mean "control." Internally, controls are handled just like windows.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date