Click here to Skip to main content
11,642,233 members (62,321 online)
Click here to Skip to main content

Drawing lines, shapes, or text on bitmaps

, 12 Apr 2002 Zlib 214K 19 98
Rate this:
Please Sign up or sign in to vote.
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:

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

From hbitmap we can extract the basic attributes:

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.

//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 were you can draw lines, text, or images. For example, the next lines draw a string over a background image :

//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:

DeleteObject(hbitmap);
hbitmap=TmpBmp;

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

//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 zlib/libpng License

Share

About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
wanggxii19-Jan-13 23:13
memberwanggxii19-Jan-13 23:13 
BugThe codes have an error? Pin
yiruirui21-Mar-12 20:19
memberyiruirui21-Mar-12 20:19 
GeneralMy vote of 5 Pin
Rohit Vipin Mathews13-Feb-12 1:00
memberRohit Vipin Mathews13-Feb-12 1:00 
GeneralThe source on the page is not working for me Pin
JJeffrey25-Nov-08 21:37
memberJJeffrey25-Nov-08 21:37 
GeneralRe: The source on the page is not working for me Pin
ddas-edEn9-Feb-12 22:44
memberddas-edEn9-Feb-12 22:44 
GeneralRe: The source on the page is not working for me Pin
ddas-edEn9-Feb-12 22:48
memberddas-edEn9-Feb-12 22:48 
QuestionCreating Text on bitmap in C# winforms Pin
pyar12323-Sep-08 20:54
memberpyar12323-Sep-08 20:54 
GeneralTEXT OVER BITMAP Pin
pelis30-May-07 7:09
memberpelis30-May-07 7:09 
Question[C#] How to move a line using the mouse after drawing it ? Pin
aliaamonier22-Apr-07 14:56
memberaliaamonier22-Apr-07 14:56 
GeneralRe: [C#] How to move a line using the mouse after drawing it ? Pin
Hamid.3-Jan-08 18:53
memberHamid.3-Jan-08 18:53 
QuestionWhere is the code source ?? Pin
mccool28-Mar-06 1:06
membermccool28-Mar-06 1:06 
AnswerRe: Where is the code source ?? Pin
Hamid.3-Jan-08 18:53
memberHamid.3-Jan-08 18:53 
QuestionDrawing shapes or bitmaps on bitmaps Pin
Assaf Koren21-Jan-06 1:14
memberAssaf Koren21-Jan-06 1:14 
AnswerRe: Drawing shapes or bitmaps on bitmaps Pin
Stan Shannon21-Jan-06 2:23
memberStan Shannon21-Jan-06 2:23 
GeneralRe: Drawing shapes or bitmaps on bitmaps Pin
Assaf Koren21-Jan-06 16:54
memberAssaf Koren21-Jan-06 16:54 
GeneralBUG Pin
navratil10-Aug-04 6:00
membernavratil10-Aug-04 6:00 
Generalresizing of images and shapes as in word art Pin
sreemail15-Dec-02 1:23
membersreemail15-Dec-02 1:23 
GeneralRe: resizing of images and shapes as in word art Pin
Davide Pizzolato15-Dec-02 1:48
memberDavide Pizzolato15-Dec-02 1:48 
Generaltext rendering with cximage Pin
Davide Pizzolato13-Apr-02 23:24
memberDavide Pizzolato13-Apr-02 23:24 
GeneralRe: text rendering with cximage Pin
Anonymous23-Apr-02 6:54
memberAnonymous23-Apr-02 6:54 
GeneralRe: text rendering with cximage Pin
Anonymous23-Apr-02 9:18
memberAnonymous23-Apr-02 9:18 
GeneralRe: text rendering with cximage Pin
Anonymous23-Apr-02 9:17
memberAnonymous23-Apr-02 9:17 
GeneralRe: text rendering with cximage Pin
Davide Pizzolato25-Apr-02 5:42
memberDavide Pizzolato25-Apr-02 5:42 
GeneralSome issues Pin
Christian Graus11-Apr-02 14:39
memberChristian Graus11-Apr-02 14:39 
GeneralRe: Some issues Pin
KarstenK11-Apr-02 23:26
memberKarstenK11-Apr-02 23:26 

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 | Terms of Use | Mobile
Web02 | 2.8.150731.1 | Last Updated 13 Apr 2002
Article Copyright 2002 by Davide Pizzolato
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid