GDI and GDI+ from C++

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+ ?

Thanks!
tullheadAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sarabandeCommented:
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
      https://msdn.microsoft.com/en-us/library/ms533798(v=VS.85).aspx
      https://msdn.microsoft.com/en-us/library/ms533799(v=vs.85).aspx
      https://msdn.microsoft.com/en-us/library/ms533969(v=vs.85).aspx

Sara
0
ZoppoCommented:
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()
{
	Gdiplus::GdiplusStartup(&gdiToken,&gdiSI,NULL);
}

// call this when you don't need GDI+ anymore
void ReleaseGdiPlus()
{
	if ( NULL != gdiToken )
	{
		Gdiplus::GdiplusShutdown(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 https://msdn.microsoft.com/de-de/library/windows/desktop/ms534077(v=vs.85).aspx

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 https://msdn.microsoft.com/en-us/library/windows/desktop/ms534127(v=vs.85).aspx
- 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 https://msdn.microsoft.com/en-us/library/windows/desktop/ms534433(v=vs.85).aspx
- 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: https://msdn.microsoft.com/en-us/library/windows/desktop/ms533799(v=vs.85).aspx

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): https://www.codeproject.com/Articles/42529/Outline-Text


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,

ZOPPO
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tullheadAuthor Commented:
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?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

ZoppoCommented:
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,

ZOPPO
0
sarabandeCommented:
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",

Sara
0
ZoppoCommented:
Sorry ;o)
0
tullheadAuthor Commented:
Thanks to both sarabande and Zoppo!   I will be trying GDI+ now... you may see a follow-on question...
0
tullheadAuthor Commented:
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
0
ZoppoCommented:
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
0
tullheadAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.