GDI and GDI+ from C++

tullhead used Ask the Experts™
Maybe a dumb question, but here goes: So, my C++ application uses GDI calls to draw pretty simple lines, arc, etc. overlaid on images.  I'd like to upgrade the look of those graphics -- maybe with something a bit more powerful than GDI that can maybe do anti-aliasing, or other things to make the overlaid lines and circles look better and cute.  So, I see talk of GDI+.  And I saw a post of a GDI+ example, and the calls look exactly like the calls that I have been referring to as "GDI" for years.   So, my application is MS VC++  and using IDS VS2015.   Its not .NET -- just C++ code, often quite close to C code actually.  So my questions:

1) Do I have to do something (link something or load something) to get GDI+?  Or is it just there, ready for me to start calling?
2) I believe I can call things in GDI+ from my non-.NET C++ code, correct?
3) Will I find some things in GDI+ that were not in GDI that will help me make my graphics (overlaid on images) look better, cuter?
4) Where do I find doc so I can look thru what is available in GDI+ ?

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
1) GDI+ is already there (since XP) . it has redeemed old GDI completely.

2) it  is independent of .NET

3) you surely you may find more and better than that you currently were using.

4) check msdn

Hi tullhead,

1. There's not much needed to make GDI+ available in your program. Just include one file, link to one lib, and some lines of code for initialization, i.e.:
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")

static Gdiplus::GdiplusStartupInput gdiSI;
static ULONG_PTR gdiToken = NULL;
static ULONG_PTR gdiHookToken = NULL;

// call this before calling any other GDI+ functionality
void InitGdiPlus()

// call this when you don't need GDI+ anymore
void ReleaseGdiPlus()
	if ( NULL != gdiToken )

Open in new window

This is the easiest way to initialize GDI+, depending on your needs there are some maybe interesting options - for further info read

Note: GDI+ needs to be initialized per thread.

2. Yes, the complete functionality is available in unmanaged C++ in namespace Gdiplus

3. Yes, there are many improvements compared to GDI, i.e.:

- easier handling of GDI+ objects since each and everything is encapsulated in C++ classes, i.e. pens, brushs, bitmaps, fonts, drawin-contexts.
- more flexibility/options for pens and brushs, i.e. shown at
- some additional drawing functions like i.e.: Gdiplus::Graphics::DrawPie
- gradient filling for brushs
- anti-aliasing for every vector drawing functions like lines, arcs, paths.
- transformation functions for geometry and color using matrices
- alpha channel is supported for (as far as I know) all drawing functions, so it's i.e. possible to draw semi-transparent PNGs or a filled polygon with semi-transparent filling.
- support for many bitmap file formats, beyond them i.e. specials like mulit-page TIFFs
- ready-to-use image manipulating effects like Blur, Sharpen, Redeye-Correction, ... - see
- last but not least: IMO one of the greatest change IMO is that drawing objects/flags/informations are de-coupled from the drawing-context, i.o.w. instead of 'selecting' i.e. a HPEN in a HDC in order to draw a line of a special color (which is error prone, because forgetting to 'un-select' it before the HDC is destroyed it generated GDI-resource leaks) in GDI+ simply a pen-object (Gdiplus::Pen) to drawing functions.

This is all I just have in mind, there most probably are some more benefits.

Notes: Another thing I'd like to mention is there are even some things which aren't possible with GDI+ allthough they are in GDI, i.e. capturing a screenshot or drawing in XOR mode. But this usually is no problem because it's possible to use GDI and GDI+ side by side, so i.e. if you need to draw something in XOR mode just retrueve the HDC of the GDI+ drawing context (Gdiplus::Graphics::GetHDC) and draw onto this HDC as usual with GDI functions.

Further I experienced some pitfalls which I didn't expect, i.e. I expected all GDI+ objects to behave the same when I create copies, no matter if I copy a Pen or a Bitmap, but I found internally the copied bitmap still references the source, so changing the copy even affected the source. Unfortunateley I wasn't able to find a GDI+ function to really low-level copy (i.o.w. by direct bitmap-data access) an image so I had to write my own (which is even easy since there are hundreds of samples in the internet).

4. A good overview of GDI+ classes, functions, a.s.o. is (of course) MSDN:

For samples and tutorials I would suggest to just search the internet for i.e. 'gdiplus tutorial c++', you'll find a lot.

I myself learned most from examples. I.e. I had to implement drawing texts with anti-aliased outline in another color, this is no really possible with GDI (the only way I can imagine is to draw in a larger bitmap and implement you own anit-aliasing down-sizing function), with a short search I found following which demonstrates nearly everything what's possible in GDI+ (including probability to compare with best possible GDI implementation):

Ok, please excuse the length of this comment, usually I don't write so much, but GDI+ is a extensive drawing library and I think it's hard to describe it in short.

I hope this helps,

best regards,



Ok, GDI+ sounds exactly good for what I am looking for;  however, perhaps I found a problem:  is it true that GDI+ requires that the project be Unicode?   I have a large project in MBCS which is a pain to convert.  What do you know?  Is Unicode required?
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

That's not true, it's no problem to use GDI+ in none-UNICODE application.

Only when drawing text the encoding is relevant, since all involved GDI+ functions expect wide-strings (UTF-16) as parameters, no matter whether the application is built with or without UNICODE.

So just when using functions like DrawString or MeasureString the passed strings need to be converted.

There are different ways for such a conversion, if you don't yet have a functionlity for this or can't find one please tell.

Best regards,

Top Expert 2016

Zoppo was much faster than me ....

you need to pass text as wide char strings. that is a little bit easier if using UNICODE strings anyway in the project. otherwise you need to convert ansi strings to wide strings what could be done by functions like mbstowcs or by using a helper string class like _bstr_t. if passing literals you simply pass like L"Hello World",

Sorry ;o)


Thanks to both sarabande and Zoppo!   I will be trying GDI+ now... you may see a follow-on question...


I've attempted to add a GDI+ test to my application.   It all runs with no crashing, but nothing draws....    my existing code passes in a CDC* called pDC  -- so I do

Graphics graphics(pDC->GetSafeHdc());

I'm wondering if its something wrong with this handling of CDC* and hdc?   If I put in the old GDI called here, operating on pDC everything works...  so, what could it be?

I have done the init code, and I have the lib, .h, and dll all in place
Hm - unfortunateley I just leave for weekend now, so I can't help a lot right now.

Just a hint: I think it might be a good idea to write a none-MFC test application which you can use in future to test different things regarding GDI+. With such a test application you can prepare code, if it works there you can copy the code to your main application and see, if it works.

If it doesn't the reason is some kind of difference between the applications' environments or something.

Beside this I can only ask you to post the drawing code you tested, but I cannot promise to take a look at it before monday ...

Best regards,



ZOPPO - I think I found the bug -- I was passing an old COLORREF as an argument into the Pen constructor -- this didn't generate any complaints, but just caused an invisible Pen.  Once I tried with Color(255,255,0) then it worked!   So, I think I am on my way now.... thanks for your great help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial