Working with Graphic Design Interface GDI+ in .Net

GDI stands for Graphic Device Interface and GDI+ is a higher level interface of GDI. GDI+ is an easy to use version of GDI. In GDI you need to know the drivers and the devices to work with but in GDI+ you need not know anything about the devices used for display and printing of data.

GDI+ takes care of everything. Hence GDI+ is more sophisticated and flexible at the same time. This higher level API allows you to set some properties of the controls to get the task done, like changing the colors and fonts of a control.

Improved colors, anti aliasing support, gradient brushes, transformation, alpha blending are some of the features that have been added in GDI+. .Net provides some namespaces to work with GDI+. The namespaces like System.Drawing, System.Drawing.Design, System.Drawing.Printing, System.Drawing.Imaging, System.Drawing.Drawing2D and System.Drawing.Text are used for working with GDI+.

The GDI+ classes can be categorized into three broad categories namely, 2-D vector graphics, Imaging, and Typography. The 2-D vector graphics classes are used to draw primitives such as line, curves, and figures which are set by points on a coordinate system. The imaging classes provide ways to display, manipulate and save bitmap images. The typography classes are related to the display of text using different fonts, sizes, and styles. To render text smoother on LCD screens a feature called sub-pixel anti aliasing is used.

The necessary basic classes of GDI+ are provided in the System.Drawing namespace. Some of the classes of System.Drawing namespace are Bitmap, Image, Brush, Brushes, Font, FontFamily, Graphics, Pen, SolidBrush, and TextureBrush. The structures found in the System.Drawing namespace are Color, Point, PointF, Rectangle, RectangleF, Size, and SizeF. The ‘F’ after the name of the structure represents that the structure is capable of handling floating point values. Thus structure like PointF, RectangleF, and SizeF can handle floating point values.

The System.Drawing.Design namespace provides classes that extend design-time user interface logic and drawing. Some of the classes of System.Drawing.Design are BitmapEditor, TextEditor, ImageEditor, ToolboxItem, and ToolBoxItemCollection. The BitmapEditor, and ImageEditor are used to select the Bitmap files and the Image files from the properties window. The ToolBoxItem class is used to implement a basic toolbox item and the ToolBoxItemCollection class is used to represent a collection of toolbox items.

The System.Drawing.Drawing2D namespace provides classes and enumerations for graphics functionality. Some of the classes in this namespace are Blend, ColorBlend, GraphicsPath, HatchBrush, LinearGradientBrush, PathGradientBrush, and Matrix. The Enumerations of this class that are commonly used are CombineMode, CompositingMode, CompositingQuality, DashCap, DashStyle, FillMode, HatchStyle, LinearGradientMode, LineCap, LineJoin, QualityMode and SmoothingMode.

The System.Drawing.Imaging namespace contains classes for imaging functionalities. The System.Drawing.Printing namespace contains classes that are used for printing functionalities. Some of the classes of System.Drawing.Printing namespace are PageSettings, PaperSize, PreviewPageInfo, PrintDocument and PrinterSettings. For typographic functionalities in your applications you can use the System.Drawing.Text namespace.

The Graphic Class is the basis for any drawing in GDI+. The graphics class is used to create a canvas for drawing any object. Hence to draw any object you have to use the graphics class. You can get a graphics object on your form’s paint event or by overriding OnPaint() method of a form. Both these require an argument of type PaintEventArgs. You can call this arguments graphics member to draw a graphics object in your application.

Public Sub DrawLinesAndCurves(e As PaintEventArgs)
‘Create a graphics surface
Dim g As Graphics = e.Graphics

' Create pens to draw lines and curves
Dim RPen As New Pen(Color.Red, 3)
Dim BPen As New Pen(Color.Blue, 3)

' Create points for the curve
Dim p1 As New Point(70, 40)
Dim p2 As New Point(110, 25)
Dim p3 As New Point(220, 10)
Dim p4 As New Point(270, 40)
Dim p5 As New Point(310, 90)
Dim p6 As New Point(360, 180)
Dim p7 As New Point(270, 250)

Dim joinPoints As Point() = {p1, p2, p3, p4, p5, p6, p7}

' Draw lines between points to screen
g.DrawLines(RPen, joinPoints, 7)

' Draw curve using the points to screen.
g.DrawCurve(BPen, joinPoints, 7)

End Sub

The above code gives you the steps necessary to draw lines between an array of points and to draw a curve joining those points. To achieve this first we created a graphics surface using the code,

Dim g As Graphics = e.Graphics

In this code ‘e’ is the argument of type PaintEventArgs that is passed to the function / subroutine. By calling the Graphics member of this argument we are creating a graphics surface for our drawing. As a next step we are creating the pens required for the drawing.

Here we have created two pens. One is of Red color and the other is of Blue color. The width of the stroke of the pen is specified as 3 pixels here. The default value of the width is 1.0. Next you create an array of points that would be used to draw lines and curves. Then you use the Drawlines and DrawCurve methods to draw lines between the points and to draw a curve connecting those points.

You can try using the other methods of the graphics class such as DrawEllipse, DrawImage, DrawPath, DrawPie, DrawPolygon, FillEllipse, FillPath, FillPie, FillPolygon, and FillRectangle.

Thus you can use the GDI+ classes to work with drawings, images, and text. You may refer to the syntax of the methods available in MSDN on the Microsoft websites or with the Help feature that is available with the Visual Studio .Net.

“Amazon and the Amazon logo are trademarks of, Inc. or its affiliates.”

| Privacy Policy for | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.