Click here to Skip to main content
Click here to Skip to main content

CBitmapDC - An automatic memory DC wrapper

By , 24 Nov 1999
 
  • 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

    About the Author

    Anneke Sicherer-Roetman
    Web Developer
    Netherlands Netherlands
    Member
    No Biography provided

    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.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    GeneralLicensingmemberAnnaPe18 Apr '11 - 4:26 
    Hello!
     
    Do you have any useage terms or license associated with CBitmapDC? I'm interested in using this in my work, but I need to check with the legal team first to see if it's OK to use. Is it free to use and distribute with commercial products?
     
    Thanks for your help!
    Anna
    QuestionHow to display a bitmap image into a dialog in vc++/mfcsussAnonymous2 Apr '05 - 23:49 
    hi
     
    can u tell me, how to display a bitmap image into a dialog in vc++/mfc.
     
    my id: senthil.ramani@gmail.com
     
    thanks in advance.
    v.senthil
    GeneralPrintingmemberGavriloaie Andrei11 Nov '04 - 7:25 
    Can I use it for printing purposes? My environement:
     
    -- Map mode is MM_HIGHMETRIC
    -- the y coordinates are inverted
     
    As far as i know BitBlt works in pixels not in logical device units (0.01 mm in my case)
     
    Why 6 is affraid of 7?
    Because 7 8 9
    Questionhow to load Bitmap after a image manipulation?membergilazilla18 Aug '04 - 4:47 
    Hi,
    i try to modify the following codes to display only gray scale image.So far my program just display a blank bitmap.Anyone can please help me.i try so many ways for a few days but still cannot.
     
    void CMy2ndtryView::OnDraw(CDC* pDC)
    {
    CMy2ndtryDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    // TODO: add draw code for native data here
     

    CDC MemDC;
    MemDC.CreateCompatibleDC (NULL);
    MemDC.SelectObject (&m_Bitmap);
     
    pDC->BitBlt
    (20,
    20,
    m_BitmapWidth,
    m_BitmapHeight,
    &MemDC,
    0,
    0,
    SRCCOPY);


    CDC resultDC;
    CBitmap m_result;
     
    m_result.CreateCompatibleBitmap(pDC,m_BitmapWidth,m_BitmapHeight);
    resultDC.CreateCompatibleDC (NULL);
    resultDC.SelectObject (&m_Bitmap);

    for( int y = 0; y < m_BitmapHeight ; y++ )
    {
    for( int x = 0; x < m_BitmapWidth ; x++ )
    {
    //set pixel to color returned by convolve
    resultDC.SetPixel(x,y,Convolve(&MemDC,x,y,kernel));
    }
    }
     

    //Display Result
    pDC->BitBlt
    (300,
    20,
    m_BitmapWidth,
    m_BitmapHeight,
    &resultDC,
    0,
    0,
    SRCCOPY);
     

     
    }
     
    COLORREF CMy2ndtryView::Convolve(CDC *pDC, int sourcex, int sourcey, float kernel[][3])
    {
    float rSum = 0, gSum = 0, bSum = 0, kSum = 0;
    float grayscale =0;
    COLORREF clrReturn = RGB(0,0,0);
    for (int i=0; i <= 2; i++)//loop through rows
    {
    for (int j=0; j <= 2; j++)//loop through columns
    {


    COLORREF tmpPixel = pDC->GetPixel(sourcex+(i-(2>>1)),sourcey+(j-(2>>1)));

    float fKernel = kernel[i][j];

    rSum += (GetRValue(tmpPixel)*fKernel);
    gSum += (GetGValue(tmpPixel)*fKernel);
    bSum += (GetBValue(tmpPixel)*fKernel);

    kSum += fKernel;
    }
    }

    if (kSum <= 0)
    kSum = 1;

    rSum/=kSum;
    gSum/=kSum;
    bSum/=kSum;
    if (rSum > 255)
    rSum = 255;
    else if (rSum < 0)
    rSum = 0;
    if (gSum > 255)
    gSum = 255;
    else if (gSum < 0)
    gSum = 0;
    if (bSum > 255)
    bSum = 255;
    else if (bSum < 0)
    bSum = 0;

    grayscale=0.299*rSum + 0.587*gSum + 0.114*bSum;
    rSum=grayscale;
    gSum=grayscale;
    bSum=grayscale;
     
    clrReturn = RGB(rSum,gSum,bSum);
    return clrReturn;
     

    }
     
    Is there something with my formula for grayscale?
    grayscale=0.299*rSum + 0.587*gSum + 0.114*bSum;

    Generalthanks very muchmemberphilipcunningham12 Apr '04 - 2:16 
    It may show how little I know about Bitmaps but this class is a real godsend. We have a large vector drawing to display and it takes forever, now we can generate a CBitmapDC copy of it and just BitBlt it instead.
     
    Many thanks for this great idea
    Phil
    GeneralWindows CEmemberJoã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
    GeneralCBITMAPDCmemberMaverick1 Jan '02 - 4:00 
    I need to draw lines and rectangles into a DC... and then I want to save it into a bitmap...I think this class will make my job very easy!!!Smile | :)
    What do you think??
    GeneralSame thing using CMemDC...memberAnonymous19 Aug '01 - 22:14 
    CMemDC is easily extended to do this.
     


    QuestionHow can i do itsussYoo28 Sep '00 - 17:27 
    I want save bitmapDC to BMP
    AnswerRe: How can i do itmemberAnneke Sicherer-Roetman28 Sep '00 - 23:52 
    bool SaveBitmap(const CDC &DC, const CString &filename)
    {
    // Get bitmap that is currently selected in DC.
    CBitmap *pBitmap = DC.GetCurrentBitmap();
     
    // Create a temporary bitmap compatible with client DC
    CClientDC clientDC(::AfxGetMainWnd());
    CBitmap TmpBitmap;
    TmpBitmap.CreateCompatibleBitmap(&clientDC, 2, 2);
     
    // Replace the bitmap in the DC, even though the DC is const.
    CDC& ncDC = const_cast(DC);
    ncDC.SelectObject(TmpBitmap);
     
    // Save the bitmap that is now deselected from the DC.
    bool res = SaveBitmap(*pBitmap, filename);
     
    // Select the bitmap in the DC again.
    ncDC.SelectObject(pBitmap);
     
    return res;
    }
     
    bool SaveBitmap(const CBitmap &Bitmap, const CString &filename)
    {
    CDIBSectionLite dibsection;
    dibsection.SetBitmap(HBITMAP(Bitmap.GetSafeHandle()));
    return dibsection.Save(filename) == TRUE;
    }
     
    The CDIBSectionLite class can also be found on the CodeProject website.

    GeneralRe: How can i do it - bugfixsussAnonymous27 May '03 - 9:25 
    This is wrong:
     
    CDC& ncDC = const_cast(DC);
     
    And should be
     
    CDC& ncDC = const_cast(DC);

    GeneralRe: How can i do it - bugfixmemberAnneke Sicherer-Roetman27 May '03 - 20:57 
    Anonymous wrote:
    This is wrong:
     
    CDC& ncDC = const_cast(DC);
     
    And should be
     
    CDC& ncDC = const_cast(DC);

     
    I don't see the difference in the wrong and the right line!
    Confused | :confused:
    GeneralRe: How can i do it - bugfixmemberBarryHolleran17 Feb '04 - 4:56 
    I agree that there is no differance in the right and wrong code, however I had a problem compiling the code at this line and I think it should be.
     

    CDC& ncDC = const_cast(DC);
     

    If I am wrong please correct me. But it does now compile.
    GeneralRe: How can i do it - bugfixmemberBarryHolleran17 Feb '04 - 5:01 
    I can now see the problem, it has lost some of the code i posted, as I think happened to the original post.
    I will try to clarify
     
    should be "CDC& ncDC = const_cast" then "" then "(DC);"
     
    The bit in the middle should read, "Less then symbol CDC Ampersand symbol Greater then symbol"
     
    I will try the whole line in quotes, i think it is because it treats it as an HTML tag.
     
    should be "CDC& ncDC = const_cast(DC);"
     
    hope this helps.Big Grin | :-D
    GeneralRe: How can i do it - bugfixmemberWolfgang Kleinschmit5 Apr '04 - 23:21 
    What about this:
    CDC& ncDC = const_cast<CDC&>(DC);
    GeneralRe: How can i do itmemberJHAKAS17 Apr '04 - 20:50 
    Thanks a lot for your quite usefule code and suggestion.
    Well i was fixed up in how to convert the CDC content to Bmp file. well this code piece does solve the problem
     
    Can you help me in understand the code
     
    NOTE:
    My bmp file is taking 2/3 rd part of page only
    My dimensions of bmp are 860*1100
     
    I am using ImageHandler library to convert the cdc to JPEG , here also i pass CDC only and it makes jpeg which takes complete page.
     

    Well can you help me out to make the bitmap file also filling up the complete page
     
    Thanks again
     
    Leave your mark wherever you go

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Permalink | Advertise | Privacy | Mobile
    Web01 | 2.6.130523.1 | Last Updated 25 Nov 1999
    Article Copyright 1999 by Anneke Sicherer-Roetman
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid