Copy CView contents to a bitmap

Posted on 2011-10-17
Last Modified: 2013-11-20
I'd like to create a bitmap copy of the current contents of my CView, which may or may not be currently visible on the screen. Here is the code I've added to my OnDraw function:

    CDC* pDC

    ... normal processing ...

    // Copy the view contents to a bitmap.

    CDC dc;
    dc.CreateCompatibleDC( pDC );

    if ( m_pBitmap != NULL )
        delete m_pBitmap;

    m_pBitmap = new CBitmap();
    m_pBitmap->CreateCompatibleBitmap( &dc,, );

    CBitmap* pOld = ( CBitmap* )dc.SelectObject( m_pBitmap );
    dc.BitBlt( 0, 0,,, pDC, 0, 0, SRCCOPY );
    dc.SelectObject( pOld );

But now when I examine the bitmap I can see that there is only 1 bit per pixel. I'm sure I'm missing something, can anyone help?

Thanks, Kevin
Question by:kjc1111
    LVL 32

    Accepted Solution

    the bitmap you get is compatible to the device context you passed in as first argument to CreateCompatibleBitmap. here you used &dc which is compatible with pDC. the pDC should be compatible with your screen device. so it should be the pointer passed to OnDraw as argument and not a new (monochrome) device context.


    Author Comment

    Thanks Sara, that worked great!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
    If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now