Working with GDI+ – Part 1

While working on my C++ projects I have to use GDI+ to handle graphics objects. Here I describe about GDI+ and how we can use it to our projects.

What is GDI+?

Microsoft Windows GDI+ is a class-based API for C/C++ programmers to develop all windows based applications. Applications based on the Microsoft Win32 API do not do not access graphics hardware directly. GDI+ interacts with device drivers on behalf of applications. It enables applications to use graphics and formatted text on both the video display and the printer. GDI+ is also supported by Microsoft Win64.

The services of Microsoft Windows GDI+ fall into the following three broad categories:

2-D vector graphics



2-D vector graphics

It helps developers to drawing primitives such as lines, curves and figures which are specified by sets of points on a coordinate system. GDI+ provides classes that store information about the primitives themselves, classes that store information about how the primitives are to be drawn, and classes that actually do the drawing. For example, the Rect class stores the location and size of a rectangle; the Pen class stores information about line color, line width, and line style; and the Graphics class has methods for drawing lines, rectangles, paths, and other figures. There are also several Brush classes that store information about how closed figures and paths are to be filled with colors or patterns.


Any kind of pictures and digital images are stored as bitmaps, arrays of numbers which represent the colors of individual dots on the screen. So data structures that store information about bitmaps are more complex and GDI+ has several classes to provide that kind of services.


Typography is concerned with the display of text in a variety of fonts, sizes, and styles. GDI+ provides an impressive amount of support for this complex task. One of the new features in GDI+ is sub-pixel anti-aliasing, which gives text rendered on an LCD screen a smoother appearance.

The Structure of GDI+

The C++ interface to Microsoft Windows GDI+ contains about 40 classes, 50 enumerations, and 6 structures. There are also a few functions that are not members of any class. The Graphics class is the core of the GDI+ interface; it is the class that actually draws lines, curves, figures, images, and text.

Graphics class has methods to drawing objects as and when required. For example, the Graphics::DrawLine method receives a pointer to a Pen object, which holds attributes (color, width, dash style, and the like) of the line to be drawn. The Graphics::FillRectangle method can receive a pointer to a LinearGradientBrush object, which works with the Graphics object to fill a rectangle with a gradually changing color.

Many classes work together with the Graphics class. Like rect, point, size, BitmapData, Bitmap etc. Rect, Point and Size class serve general purpose and Others are for specialized purposes and considered as helper classes.

GDI+ defines several enumerations, which are collections of related constants. For example, the LineJoin enumeration used to join two lines. It contain three elements which specify line join styles- LineJoinBevel, LineJoinMeter and LineJoinRound.

GDI+ provides a few functions that are not part of any class. Two of those functions are GdiplusStartup and GdiplusShutdown. You must call GdiplusStartup before you make any other GDI+ calls, and you must call GdiplusShutdown when you have finished using GDI+.

Lets start working:

1. Install the latest Microsoft SDK which contain GDI+ header file and prepared a directory with all the GDI+ headers. Keep it within your application folder.

Go to Project->Settings->Link from main menu

remove Debug/ from output filename field to keep exe file into application path:


Enter following line into Object/library module field:


now press Ok button

2. Add the following code into StdAfx.h file

#include “GDIPlus/GBGdiPlus.h”
using namespace Gdiplus;

3. Download gdiplus.dll and paste it to your application folder(The safest place to put it in the same directory as your executeble).

Click here to go to the download of gdiplus.dll

4. add code to your projects App files :

#include “stdafx.h”
#include “ImageViewer.h”
#include “ImageViewerDlg.h”

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;



BOOL ImageViewerApp::InitInstance()

if (!AfxSocketInit())
return FALSE;


//begin: initialize GDI+
GdiplusStartupInput GDIPlusStartupInput;
VERIFY(GdiplusStartup( &GDIPlusToken, &GDIPlusStartupInput, NULL ) == Ok );
//end: initialize GDI+

return FALSE;


int ImageViewerApp::ExitInstance()
// TODO: Add your specialized code here and/or call the base class
//begin: shutdown GDI+
//end: shutdown GDI+

return CWinApp::ExitInstance();

Now GDI+ is ready to work with your project.

To test it you may add some code within paint function of your dialog

void ImageViewerDlg::OnPaint()
CPaintDC dc(this);
Graphics graphics(dc);
Pen pen1(Color(255, 255, 255), 400);
graphics.DrawLine(&pen1, 200, 0, 200, 400);

Pen pen1(Color(0, 0, 0), 10);
graphics.DrawLine(&pen1, 133, 0, 137, 400);


Pen pen1(Color(0, 0, 0), 10);
graphics.DrawLine(&pen1, 266, 0, 270, 400);

5. Compile and run the project. display two virticle line in your dialog.


One Response

  1. Only want to say your article is as Great. The clearness in your post is simply spectacular and i can take for granted you are an expert on this field. Well with your permission allow me to grab your rss feed to keep up to date with succeeding post. Thanks a million and please keep up the accomplished work.from

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: