Click here to Skip to main content
11,505,744 members (57,745 online)
Click here to Skip to main content

Using GDI+ with MFC or native C/C++

, 14 Apr 2003 203.8K 3.2K 81
Rate this:
Please Sign up or sign in to vote.
Why 99.9% of GDI+ samples in .NET languages? What about C/C++?

Introduction

If you did a GDI+ sample query on Google, I bet you will get hundreds or thousands of URLs pointing you to a .NET free code. If you go book shop you will see dozens of books talking about GDI+. However, if you try to find information about integrating GDI+ with MFC/ATL/WTL, I bet you will find none (if you could, kindly post the URL!). So, is it impossible or so difficult that it doesn't worth using?

Findings

The answer is: Extremely easy to use GDI+ in MFC/ATL/WTL, or even straight C language. First, I have to admit I have no knowledge about .NET, I am not a fan of Microsoft .NET but rather prefer platform SDK. GDI+ IS NOT solely for .NET, if you plan to use GDI+ on Windows version lower than XP, then you would have to redistribute the GDI+ runtime (here). Don't worry, it is only 1.04 MB, not a xx MB .NET runtime.

So?

  • Add the following line to your stdafx.h
    #include <gdiplus.h>
    using namespace Gdiplus;
    #pragma comment(lib, "gdiplus.lib")
  • Intialize the GDI+ resources. Add this to your CWinApp derived class as member:
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;

    At InitInstance(), add:

    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  • Your application is ready to consume GDI+ now.
  • Upon exit, release GDI+ resources. Add the following line to ExitInstance():
    GdiplusShutdown(gdiplusToken);

As you can see, it is that simple. In general, it should be very easy to port your existing WIN32/MFC/WTL painting code into GDI+. If you ask why? Consider there is one GDI+ C++ class called RedEyeCorrection, that's right, to correct the red eyes that sometimes occur in flash photographs. If you have at least one year WIN32/MFC experiences, just follow the link here to see more.

I included a demo project to demonstrate the above. Enjoy!

GDI+ is device context handle centric, hence if you can split drawing logic aside from actual drawing code, you will have no problem in porting to GDI+. In some cases, 100 lines of drawing code can be reduced to just 5 lines with GDI+ intensive support of complex drawing exercises. This means mixing ordinary GDI with GDI+ a trivial task.

The downloaded project is a standard MFC SDI project, if you have read it (I hope so...), you will find that it is unbelievable simple. Nearly all time, you can simply cut-n-paste source code embedded in MSDN GDI+ to try things out. One tips: if the GDI+ need to load an external file, you can just right click the image in the help and save it, then continue to use the downloaded project to continue testing.

Finally, you do need to have a copy of platform SDK to have necessary GDI+ header, library at your VC search path. It is not necessary for the latest version, I am using version Aug'02. GDI+ files can be extracted from CAB_11, if you do not want to install the SDK.

** Don't forget to visit here for more information on integrating GDI+ with Win32/MFC/ATL/WTL **

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

TW

Malaysia Malaysia
No Biography provided

Comments and Discussions

 
SuggestionNever put using namespace in a .h file Pin
Charles Owen11-Jul-14 3:46
memberCharles Owen11-Jul-14 3:46 
Questioncode sequencing Pin
Member 102995233-Oct-13 4:51
memberMember 102995233-Oct-13 4:51 
QuestionInteresting and useful article Pin
Bhushan A10-Sep-13 1:54
memberBhushan A10-Sep-13 1:54 
GeneralMy vote of 1 Pin
Sergey Alexandrovich Kryukov18-Mar-13 14:48
mvpSergey Alexandrovich Kryukov18-Mar-13 14:48 
GeneralMy vote of 3 Pin
shetkarabhijeet26-Nov-12 1:55
membershetkarabhijeet26-Nov-12 1:55 
GeneralMy vote of 5 Pin
Shaunak De21-Sep-12 23:36
memberShaunak De21-Sep-12 23:36 
QuestionBuild samples with VS2010 [modified] Pin
Will Stott13-Sep-11 6:09
memberWill Stott13-Sep-11 6:09 
GeneralGood Title, Excellent Sample Pin
Momony29-Apr-10 10:35
memberMomony29-Apr-10 10:35 
GeneralRe: Good Title, Excellent Sample Pin
Theo Buys10-Feb-11 2:24
memberTheo Buys10-Feb-11 2:24 
GeneralBitmap - delete the object Pin
Alex Evans6-Feb-05 14:56
memberAlex Evans6-Feb-05 14:56 
GeneralRe: Bitmap - delete the object Pin
TW6-Feb-05 16:40
memberTW6-Feb-05 16:40 
GeneralRe: Bitmap - delete the object Pin
Alex Evans7-Feb-05 9:25
memberAlex Evans7-Feb-05 9:25 
Generalthank very much. ^^ Pin
vpas24-Jan-05 18:28
membervpas24-Jan-05 18:28 
GeneralBest GDI+ MFC Beginners article so far Pin
TommYNandA19-Nov-04 20:37
memberTommYNandA19-Nov-04 20:37 
GeneralTo end this silly debate.... Pin
Tanzim Husain19-Nov-03 18:15
memberTanzim Husain19-Nov-03 18:15 
GeneralDo not want to debate, but... Pin
Zhefu Zhang10-Nov-03 15:11
memberZhefu Zhang10-Nov-03 15:11 
QuestionYou wanted a URL? Pin
Marc Clifton8-Nov-03 2:39
editorMarc Clifton8-Nov-03 2:39 
AnswerRe: You wanted a URL? Pin
TW8-Nov-03 2:43
memberTW8-Nov-03 2:43 
GeneralRe: You wanted a URL? Pin
Marc Clifton8-Nov-03 2:47
editorMarc Clifton8-Nov-03 2:47 
AnswerRe: You wanted a URL? Pin
vjedlicka7-Dec-05 22:56
membervjedlicka7-Dec-05 22:56 
GeneralRe: You wanted a URL? Pin
Theo Buys10-Feb-11 2:16
memberTheo Buys10-Feb-11 2:16 
GeneralRe: You wanted a URL? Pin
vjedlicka10-Feb-11 2:32
membervjedlicka10-Feb-11 2:32 
Generalgoods Pin
Anonymous14-Jun-03 21:21
sussAnonymous14-Jun-03 21:21 
GeneralGood but incomplete Pin
Nicolas Bonamy16-Apr-03 3:44
memberNicolas Bonamy16-Apr-03 3:44 
GeneralWhy 99.9% of GDI+ samples... Pin
Jim Crafton15-Apr-03 7:42
memberJim Crafton15-Apr-03 7:42 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Mario M.15-Apr-03 9:44
memberMario M.15-Apr-03 9:44 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Marc Clifton8-Nov-03 2:19
editorMarc Clifton8-Nov-03 2:19 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Mario M.8-Nov-03 3:41
memberMario M.8-Nov-03 3:41 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Marc Clifton8-Nov-03 4:24
editorMarc Clifton8-Nov-03 4:24 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Mario M.8-Nov-03 5:54
memberMario M.8-Nov-03 5:54 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Marc Clifton8-Nov-03 6:29
editorMarc Clifton8-Nov-03 6:29 
GeneralUseful link Pin
TW8-Nov-03 6:44
memberTW8-Nov-03 6:44 
GeneralRe: Useful link Pin
Mario M.8-Nov-03 13:39
memberMario M.8-Nov-03 13:39 
GeneralRe: Useful link Pin
Yves7-Jan-07 14:26
memberYves7-Jan-07 14:26 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Dieter Hammer15-Apr-03 20:08
memberDieter Hammer15-Apr-03 20:08 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Marc Clifton8-Nov-03 2:23
editorMarc Clifton8-Nov-03 2:23 
GeneralRe: Why 99.9% of GDI+ samples... Pin
TW8-Nov-03 2:48
memberTW8-Nov-03 2:48 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Marc Clifton8-Nov-03 2:51
editorMarc Clifton8-Nov-03 2:51 
GeneralRe: Why 99.9% of GDI+ samples... Pin
TW8-Nov-03 2:58
memberTW8-Nov-03 2:58 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Marc Clifton8-Nov-03 3:04
editorMarc Clifton8-Nov-03 3:04 
GeneralRe: Why 99.9% of GDI+ samples... Pin
TW8-Nov-03 3:08
memberTW8-Nov-03 3:08 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Marc Clifton8-Nov-03 6:32
editorMarc Clifton8-Nov-03 6:32 
GeneralRe: Why 99.9% of GDI+ samples... Pin
TW8-Nov-03 6:37
memberTW8-Nov-03 6:37 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Marc Clifton8-Nov-03 6:55
editorMarc Clifton8-Nov-03 6:55 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Tanzim Husain15-Apr-03 20:35
memberTanzim Husain15-Apr-03 20:35 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Vernero Cifagni15-Apr-03 23:02
memberVernero Cifagni15-Apr-03 23:02 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Marc Clifton8-Nov-03 2:29
editorMarc Clifton8-Nov-03 2:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 15 Apr 2003
Article Copyright 2003 by TW
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid