Click here to Skip to main content
Licence Zlib
First Posted 10 Apr 2002
Views 181,494
Bookmarked 84 times

Drawing lines, shapes, or text on bitmaps

By | 12 Apr 2002 | Article
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

About the Author

Davide Pizzolato



Italy Italy

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
BugThe codes have an error? Pinmemberyiruirui20:19 21 Mar '12  
GeneralMy vote of 5 PinmemberRohit Vipin Mathews1:00 13 Feb '12  
GeneralThe source on the page is not working for me PinmemberJJeffrey21:37 25 Nov '08  
GeneralRe: The source on the page is not working for me Pinmemberddas-edEn22:44 9 Feb '12  
GeneralRe: The source on the page is not working for me [modified] Pinmemberddas-edEn22:48 9 Feb '12  
QuestionCreating Text on bitmap in C# winforms Pinmemberpyar12320:54 23 Sep '08  
Hello,could anyone of you give me de soluton to have a text on the bitmap image using C#..I have created 2 parts like pie chart using bitmap class.Now i want the texts on these separate parts.How is this done??
GeneralTEXT OVER BITMAP Pinmemberpelis7:09 30 May '07  
Question[C#] How to move a line using the mouse after drawing it ? Pinmemberaliaamonier14:56 22 Apr '07  
GeneralRe: [C#] How to move a line using the mouse after drawing it ? PinmemberHamid.18:53 3 Jan '08  
QuestionWhere is the code source ?? Pinmembermccool1:06 28 Mar '06  
AnswerRe: Where is the code source ?? PinmemberHamid.18:53 3 Jan '08  
QuestionDrawing shapes or bitmaps on bitmaps PinmemberAssaf Koren1:14 21 Jan '06  
AnswerRe: Drawing shapes or bitmaps on bitmaps PinmemberStan Shannon2:23 21 Jan '06  
GeneralRe: Drawing shapes or bitmaps on bitmaps PinmemberAssaf Koren16:54 21 Jan '06  
GeneralBUG Pinmembernavratil6:00 10 Aug '04  
Generalresizing of images and shapes as in word art Pinmembersreemail1:23 15 Dec '02  
GeneralRe: resizing of images and shapes as in word art PinmemberDavide Pizzolato1:48 15 Dec '02  
Generaltext rendering with cximage PinmemberDavide Pizzolato23:24 13 Apr '02  
GeneralRe: text rendering with cximage PinmemberAnonymous6:54 23 Apr '02  
GeneralRe: text rendering with cximage PinmemberAnonymous9:18 23 Apr '02  
GeneralRe: text rendering with cximage PinmemberAnonymous9:17 23 Apr '02  
GeneralRe: text rendering with cximage PinmemberDavide Pizzolato5:42 25 Apr '02  
GeneralSome issues PinmemberChristian Graus14:39 11 Apr '02  
GeneralRe: Some issues PinmemberKarstenK23:26 11 Apr '02  
GeneralRe: Some issues PinmemberChristian Graus23:46 11 Apr '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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120604.1 | Last Updated 13 Apr 2002
Article Copyright 2002 by Davide Pizzolato
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid