Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / MFC
Article

CBitmapDC - An automatic memory DC wrapper

Rate me:
Please Sign up or sign in to vote.
4.86/5 (15 votes)
24 Nov 1999 140.8K   3.7K   49   16
A handy class that provides a memory bitmap device context
  • Download demo project - 18 Kb
  • Download source - 5 Kb
  • CBitmapDC is a subclass of CDC. It is a handy little class that provides a memory bitmap device context without having to reinvent the wheel every time you need one.

    Example 1

    CBitmapDC can be used to make a CBitmap on the fly
    1. make a memory bitmap DC by calling CBitmapDC's constructor with the desired bitmap width and height
    2. draw in this device context with normal CDC drawing functions
    3. call CBitmapDC's Close function, this returns a pointer to the finished CBitmap
    4. do whatever you want to do with the CBitmap
    5. delete the CBitmap
    void CMyView::OnDraw(CDC* pDC)
    {
      CBitmapDC bitmapDC(50, 50, pDC);
      bitmapDC.Rectangle(0, 0, 50, 50);
      bitmapDC.MoveTo(0,50);
      bitmapDC.LineTo(50,0);
      CBitmap *pbmp = bitmapDC.Close();
      DrawBitmap(pbmp, pDC, CPoint(10, 10));
      delete pbmp;
    }

    Example 2

    CBitmapDC can be used as a temporary scratchpad
    1. make a memory bitmap DC by calling CBitmapDC's constructor with the desired bitmap width and height
    2. draw in this device context with normal CDC drawing functions
    3. do whatever you want to do e.g. blit the memory DC to screen
    4. use the automatic cleanup of CBitmapDC's destructor
    void CMyView::OnDraw(CDC* pDC)
    {
      CBitmapDC bitmapDC_2(50, 50, pDC);
      bitmapDC_2.Rectangle(0, 0, 50, 50);
      bitmapDC_2.MoveTo(0, 0);
      bitmapDC_2.LineTo(50, 50);
      pDC->BitBlt(200, 10, 50, 50, &bitmapDC_2, 0, 0, SRCCOPY);
    }

    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


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

    Comments and Discussions

     
    GeneralLicensing Pin
    AnnaPe18-Apr-11 4:26
    AnnaPe18-Apr-11 4:26 
    QuestionHow to display a bitmap image into a dialog in vc++/mfc Pin
    Anonymous2-Apr-05 23:49
    Anonymous2-Apr-05 23:49 
    GeneralPrinting Pin
    Gavriloaie Andrei11-Nov-04 7:25
    Gavriloaie Andrei11-Nov-04 7:25 
    Questionhow to load Bitmap after a image manipulation? Pin
    gilazilla18-Aug-04 4:47
    gilazilla18-Aug-04 4:47 
    Generalthanks very much Pin
    philipcunningham12-Apr-04 2:16
    philipcunningham12-Apr-04 2:16 
    GeneralWindows CE Pin
    João Paulo Figueira11-Sep-03 2:53
    professionalJoão Paulo Figueira11-Sep-03 2:53 
    This code works in Windows CE (tested on the Pocket PC 2002) as long as you conditionally remove all references to SetMapMode() and GetMapMode().
    #ifndef _WIN32_WCE
      CDC::SetMapMode(pOrigDC->GetMapMode());
    #endif


    By the way: Great work! Wink | ;)

    Regards,
    João Paulo
    GeneralCBITMAPDC Pin
    Maverick1-Jan-02 4:00
    Maverick1-Jan-02 4:00 
    GeneralSame thing using CMemDC... Pin
    19-Aug-01 22:14
    suss19-Aug-01 22:14 
    QuestionHow can i do it Pin
    yoo28-Sep-00 17:27
    yoo28-Sep-00 17:27 
    AnswerRe: How can i do it Pin
    Anneke Sicherer-Roetman28-Sep-00 23:52
    Anneke Sicherer-Roetman28-Sep-00 23:52 
    GeneralRe: How can i do it - bugfix Pin
    Anonymous27-May-03 9:25
    Anonymous27-May-03 9:25 
    GeneralRe: How can i do it - bugfix Pin
    Anneke Sicherer-Roetman27-May-03 20:57
    Anneke Sicherer-Roetman27-May-03 20:57 
    GeneralRe: How can i do it - bugfix Pin
    BarryHolleran17-Feb-04 4:56
    BarryHolleran17-Feb-04 4:56 
    GeneralRe: How can i do it - bugfix Pin
    BarryHolleran17-Feb-04 5:01
    BarryHolleran17-Feb-04 5:01 
    GeneralRe: How can i do it - bugfix Pin
    Wolfgang Kleinschmit5-Apr-04 23:21
    Wolfgang Kleinschmit5-Apr-04 23:21 
    GeneralRe: How can i do it Pin
    JHAKAS17-Apr-04 20:50
    JHAKAS17-Apr-04 20:50 

    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.