Click here to Skip to main content
Click here to Skip to main content
Go to top

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

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:

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:

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.

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)

Share

About the Author

DavidCrow
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 PinmemberSyamlalS3-May-07 4:08 
GeneralRe: Not declared error Pinmemberalwittta21-Aug-07 19:41 
GeneralRe: Not declared error PinmemberRick York1-Aug-08 13:07 
JokeRe: Not declared error Pinmemberaljodav26-Sep-08 8:47 
GeneralTRANSPARENT PinmemberJohn R. Shaw3-Mar-04 9:59 
GeneralRe: TRANSPARENT PinmemberDavidCrow3-Mar-04 10:02 

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 | Mobile
Web03 | 2.8.140916.1 | Last Updated 1 Aug 2008
Article Copyright 2004 by DavidCrow
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid