Click here to Skip to main content
13,504,308 members
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 29 Feb 2004

Rendering text on top of a bitmap

, 1 Aug 2008
Rate this:
Please Sign up or sign in to vote.
Rendering text on top of a bitmap.

Sample Image - GDITest.jpg


It's not often I find myself in the need to know something GDI-related. As a matter of fact, I can count on one hand the number of times this has occurred within the past 13 years. I'm not talking about the normal everyday stuff that a Windows GUI application does, but the more involved stuff with pens, brushes, lines, bitmaps, DCs, etc. If that is the sort of thing you do on an everyday basis, then you are way beyond the scope of this article. I classified this article as "Intermediate" but it really should be between "Beginner and Intermediate," sort of a "Beginner+" classification.

Anyway, this was a fun and educational exercise in what was needed to render some text on top of a bitmap. An example of such can be found here. As an example of making a mountain out of a molehill, my first thought on how this was accomplished was to create the bitmap structure manually, add the appropriate bits for the text, and save the result to a file. I guess it would be do-able, but the means would hardly justify the end.

My next thought was that maybe something handy existed in the GDI+ API. I did find the Image and Graphics classes, with what looked like the right methods. My first attempt was unsuccessful, so I tried one last thing, only using GDI instead.

The Result

The first order of business was to load a bitmap image into the a Device Context (DC) that is compatible with the display DC. This was done using:

HBITMAP hBitmap = (HBITMAP) LoadImage(0, 
  _T("somefile.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (NULL != hBitmap)
    CBitmap Bitmap;

    CDC dcCompatible;

    CBitmap *pOldBitmap = dcCompatible.SelectObject(&Bitmap);

At this point, a temporary DC exists and contains the loaded bitmap. The next step is to render some text on that same DC. This was done using:

dcCompatible.TextOut(x, y, _T("Some text here"));

We now have text on top of a bitmap. To render the result to the display, we need to copy the contents of the temporary DC to the display DC. This was done using:

pDC->BitBlt(0, 0, width, height, &dcCompatible, 0, 0, SRCCOPY);

That's all there is to it! I did do some other things, but they were more for the sample project as opposed to the context of this article. Things like Print Preview, handling Desktops that do not have a wallpaper defined, and querying the network and workstation for useful information.


I had to consult several web sites and USENET groups to find the information I was looking for. A special thanks goes to Mike Sutton, and Johan Rosengren.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

David Crow
Software Developer (Senior) Pinnacle Business Systems
United States United States

The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.

HTTP 404 - File not found
Internet Information Services

You may also be interested in...

Comments and Discussions

GeneralNot declared error Pin
SyamlalS3-May-07 4:08
memberSyamlalS3-May-07 4:08 
GeneralRe: Not declared error Pin
alwittta21-Aug-07 19:41
memberalwittta21-Aug-07 19:41 
GeneralRe: Not declared error Pin
Rick York1-Aug-08 13:07
memberRick York1-Aug-08 13:07 
JokeRe: Not declared error Pin
aljodav26-Sep-08 8:47
memberaljodav26-Sep-08 8:47 
John R. Shaw3-Mar-04 9:59
memberJohn R. Shaw3-Mar-04 9:59 
DavidCrow3-Mar-04 10:02
memberDavidCrow3-Mar-04 10:02 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180417.1 | Last Updated 1 Aug 2008
Article Copyright 2004 by David Crow
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid