Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Rendering Text on Top of a Bitmap

0.00/5 (No votes)
1 Aug 2008 1  
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 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