Click here to Skip to main content
15,884,099 members
Articles / Desktop Programming / MFC

Drawing Lines, Shapes, or Text on Bitmaps

Rate me:
Please Sign up or sign in to vote.
4.91/5 (24 votes)
12 Apr 2002CPOL1 min read 283.6K   107   31
Quick reference to draw lines, shapes, or text on bitmaps

Introduction

This article describes the operations needed to draw lines, shapes, or text on bitmaps. The task is quite simple, but a quick reference could be handy for the beginners.

Working with HBITMAPs

As first step, we need a background image, referenced by a hbitmap handle. hbitmap can be the result of previous operations, or created with CreateBitmap(), or a resource:

C++
HBITMAP hbitmap = ::LoadBitmap(AfxGetInstanceHandle(),
        MAKEINTRESOURCE(IDB_BITMAP1));

From hbitmap, we can extract the basic attributes:

C++
BITMAP bm;
GetObject( hbitmap, sizeof(BITMAP), &bm );
long width=bm.bmWidth;
long height=bm.bmHeight;

Now let's create a memory device context and select a new bitmap.

C++
//prepare the bitmap attributes
BITMAPINFO bmInfo;
memset(&bmInfo.bmiHeader,0,sizeof(BITMAPINFOHEADER));
bmInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
bmInfo.bmiHeader.biWidth=width;
bmInfo.bmiHeader.biHeight=height;
bmInfo.bmiHeader.biPlanes=1;
bmInfo.bmiHeader.biBitCount=24;
//create a temporary dc in memory.
HDC pDC = ::GetDC(0);
HDC TmpDC=CreateCompatibleDC(pDC);
//create a new bitmap and select it in the memory dc
BYTE *pbase;
HBITMAP TmpBmp=CreateDIBSection(pDC,
    &bmInfo,DIB_RGB_COLORS,(void**)&pbase,0,0);
HGDIOBJ TmpObj=SelectObject(TmpDC,TmpBmp);

TmpDC is the device context where you can draw lines, text, or images. For example, the next lines draw a string over a background image:

C++
//draw the background
HDC dcBmp=CreateCompatibleDC(TmpDC);
HGDIOBJ TmpObj2 = SelectObject(dcBmp,hbitmap);
BitBlt(TmpDC,0,0,width,height,dcBmp,0,0,SRCCOPY);
SelectObject(TmpDC,TmpObj2);
DeleteDC(dcBmp);

//choose the font
CFont m_Font;
LOGFONT* m_pLF;
m_pLF=(LOGFONT*)calloc(1,sizeof(LOGFONT));
strncpy(m_pLF->lfFaceName,"Times New Roman",31);
m_pLF->lfHeight=64;
m_pLF->lfWeight=600;
m_pLF->lfItalic=1;
m_pLF->lfUnderline=0;
m_Font.CreateFontIndirect(m_pLF);

//select the font in the dc
CDC dc;
dc.Attach(TmpDC);
CFont* pOldFont=NULL;
if (m_Font.m_hObject) 
    pOldFont = dc.SelectObject(&m_Font);
else 
    dc.SelectObject(GetStockObject(DEFAULT_GUI_FONT));

//Set text color
dc.SetTextColor(RGB(60,120,240));
//Set text position;
RECT pos = {40,40,0,0};
//draw the text
dc.SetBkMode(TRANSPARENT);
dc.DrawText("Test",4,&pos,DT_CALCRECT);
dc.DrawText("Test",4,&pos,0);

//cleanup 
if (pOldFont) dc.SelectObject(pOldFont);
m_Font.DeleteObject();
dc.Detach();
free(m_pLF);

At this point, there are 2 bitmaps: hbitmap and TmpBmp, you can keep both the old and the new image, or replace hbitmap with TmpBmp:

C++
DeleteObject(hbitmap);
hbitmap=TmpBmp;

Finally, we can delete the temporary device context. Do not delete hbitmap and TmpBmp here, or you'll lose the bitmap.

C++
//final cleanup
SelectObject(TmpDC,TmpObj);
DeleteDC(TmpDC);

Conclusion

The article starts with a background image stored in a HBITMAP, and ends with a new image in a new HBITMAP. This grants a high flexibility on what you can do before, after and in between: you can use all the GDI functions to build your image, and for example you can use CBitmap, or image processing libraries like FreeImage and CxImage, to add effects or save the result in a file.

License

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


Written By
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: text rendering with cximage Pin
23-Apr-02 9:18
suss23-Apr-02 9:18 
GeneralRe: text rendering with cximage Pin
23-Apr-02 9:17
suss23-Apr-02 9:17 
GeneralRe: text rendering with cximage Pin
Davide Pizzolato25-Apr-02 5:42
Davide Pizzolato25-Apr-02 5:42 
GeneralSome issues Pin
Christian Graus11-Apr-02 14:39
protectorChristian Graus11-Apr-02 14:39 
GeneralRe: Some issues Pin
KarstenK11-Apr-02 23:26
mveKarstenK11-Apr-02 23:26 
GeneralRe: Some issues Pin
Christian Graus11-Apr-02 23:46
protectorChristian Graus11-Apr-02 23:46 
GeneralRe: Some issues Pin
Davide Pizzolato12-Apr-02 1:08
Davide Pizzolato12-Apr-02 1:08 
GeneralRe: Some issues Pin
Christian Graus12-Apr-02 1:33
protectorChristian Graus12-Apr-02 1:33 
Davide Pizzolato wrote:
and sometime I forget that I'm a beginner too.

Aren't we all, though ? 80)



Christian

The tragedy of cyberspace - that so much can travel so far, and yet mean so little.

"I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?!
- Jon Hulatt, 22/3/2002
GeneralRe: Some issues Pin
Jehy16-May-08 15:14
Jehy16-May-08 15:14 
GeneralRe: Some issues Pin
Davide Pizzolato17-May-08 1:13
Davide Pizzolato17-May-08 1:13 
GeneralRe: Some issues Pin
Jehy17-May-08 4:51
Jehy17-May-08 4:51 

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.