It is found
sometimes that the graphics form of data provides better mode of communication
since it can enhance the user interface of the application by representing
the data in clear, concise and lucid manner. Some of the tasks that use
graphics are inserting graphical charts and reports in documents, creating
new or editing existing images, drawing customized shapes, logos, etc.
in the documents wherever it is necessary.
framework provides umpteen numbers of options to create and edit graphics
through a variety of programming techniques. The Graphics feature in .Net
has been mostly implemented in a set of managed classes in the namespaces,
System.Drawing, System.Drawing.Drawing2D, System.Drawing.Imaging, System.Drawing.Printing
and System.Drawing.Text. These classes that help in custom drawing on
the screen forms the GDI+ (Graphics Device Interface) object model of
.Net framework. These classes are very intuitive and easy to use. The
services provided in this model can be categorized into the following
basic graphics like line, rectangle, etc.
Creating and using images, bitmaps, icons, etc.
Formatting the text content to different size and shapes
Principles of GDI+
The main aim of the GDI+ is to abstract the details of the device in which
the output is displayed and hence enable developers to build device independent
applications. GDI+ is basically derived from GDI that was included in
earlier version of Windows. GDI+ involves the device context (DC) object
which represents the device in which the output is displayed. This DC
object stores information about a particular output device (printer, monitor,
etc.) and takes care of translating the calls from the operating system
to the device. The class, System.Drawing.Graphics class wraps this functionality
of device context and is used for handling all the drawing operations.
Some of the classes that implement the logic for drawing two-dimensional
graphics are Brush, Font, Icon, Image, Pen, etc. The names of the classes
are self-explanatory to the service they provide. In addition to these
classes, the Graphics class is used to draw to the display device. The
Pen class is used to draw lines and curves while the classes derived from
Brush are used to fill the interiors of the shapes.
There are also few structures like Point, Color, Rectangle and Size which
are used to specify the details of an object which can be of more than
one value. For example, by using a Point structure, the co-ordinates of
a control (Button1) to be 10 pixels from the upper-left corner of the
form in which it resides can be specified as below:
Button1.Location = new Point (10, 10);
Following can be the steps to draw a line or any two-dimensional figure
in a Windows Form using .Net classes:
Creating a Graphics object by means of calling the System.Windows.Forms.Control.CreateGraphics
Creating the Pen object and to specify the color and width in pixels.
Draw a line or any shape by calling the Graphics member methods
and by using the Pen instance created. Some of the methods of Graphics
class are DrawEllipse, DrawImage, DrawLine, DrawPie, DrawPolygon, etc.
Each of these members is overloaded with different methods through which
the figure can be drawn.
Following are the steps to draw solid shape filled with color:
Create a Graphics object by calling the System.Windows.Forms.Control.CreateGraphics
Create a Brush object and specify the filling pattern and color
(by choosing the required derived class of Brush class).
Call the required Fill method of Brush class and then the Draw
Working with Images
The framework provides classes, Image and Bitmap for working with a variety
of image formats which need to be edited in applications. Image class
helps to create, load, modify and save drawings, add copyright information
to a picture and resize JPEG images to a lesser size for faster downloading.
Bitmap is another useful class derived from Image and can be used for
handling still images. The ability that Bitmap possess and Image class
does not is that Bitmap is able to control the Color object pertaining
to a pixel in an image to the required color.
Pictures can be displayed in a Windows form in two different ways. The
first option is to call method, FromFile of Image object and set this
as background image of a PictureBox control. The other option is to use
Bitmap object loaded with the image and pass it as parameter to the method,
DrawImage of the Graphics object.
Icons are transparent bitmaps of standard size, 40 by 40 pixels provided
by the framework. For displaying icons, method, Graphics.DrawIcon or Graphics.DrawIconUnstretched
The framework has also provided classes for adding text to images in a
variety of size, font and style for labeling and reporting purposes. The
Graphics class is mainly used for this. To write text, call the Graphics.DrawString
method by specifying the Font and Brush (Font object for specifying the
shape of the characters of text) objects with the location to draw the
text. The StringFormat class is used to specify alignment, text direction,
LineLimit, etc. and is passed to the DrawString method to display the
text in the required format.
Thus, the GDI+ model of the .Net framework facilitates as a powerful tool
to display customized and complex figures to the specific output device
through simple statements. However, it is important to understand the
principles of GDI+ and the design of Windows architecture to use it efficiently