Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / MFC

Rendering Text on Top of a Bitmap

Rate me:
Please Sign up or sign in to vote.
3.23/5 (22 votes)
1 Aug 2008CPOL2 min read 72.4K   1.2K   25   6
Rendering text on top of a bitmap

Sample Image - GDITest.jpg

Introduction

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:

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

    CDC dcCompatible;
    dcCompatible.CreateCompatibleDC(pDC);

    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:

C++
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:

C++
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.

References

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.

License

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


Written By
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

Comments and Discussions

 
GeneralNot declared error Pin
Syamlal S Nair3-May-07 4:08
Syamlal S Nair3-May-07 4:08 
GeneralRe: Not declared error Pin
alwittta21-Aug-07 19:41
alwittta21-Aug-07 19:41 
GeneralRe: Not declared error Pin
Rick York1-Aug-08 13:07
mveRick York1-Aug-08 13:07 
JokeRe: Not declared error Pin
aljodav26-Sep-08 8:47
aljodav26-Sep-08 8:47 
GeneralTRANSPARENT Pin
John R. Shaw3-Mar-04 9:59
John R. Shaw3-Mar-04 9:59 
GeneralRe: TRANSPARENT Pin
David Crow3-Mar-04 10:02
David Crow3-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.