CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2000
    Location
    Birmingham, England
    Posts
    2,520

    Create a CBitmap from a CDC ?

    Hi,

    I want to create a bitmap in memory from the contents of a device context held in memory, and I'm really struggling. Here's my CDC code :

    Code:
    		CPaintDC dc(this); // device context for painting
    
    		m_sCaption = "Some text";
    
    		CFont font;
    		font.CreatePointFont(150, _T("Verdana"));
    
    		CDC memDC;
    		memDC.CreateCompatibleDC(&dc); 
    		memDC.SetBkMode(TRANSPARENT);
    		memDC.SetTextColor(RGB(0, 0, 0));
    		memDC.SelectObject(&font);
    		memDC.TextOut(0, 0, m_sCaption);
    
    		CSize sz = memDC.GetTextExtent(m_sCaption);
    
                                    // i now have my text in a device context, and i know the size of the image. 
                                   // what code do i add here to copy this into a bitmap ?
    Thanks

    Jase

    www.slideshowdesktop.com
    View your images and photos on your desktop with ease using SlideShow Desktop, the desktop wallpaper manager for Microsoft Windows.
    ...

  2. #2
    Join Date
    Jun 2001
    Location
    UK
    Posts
    128
    I think you should create a bitmap and select it into the dc...

    e.g. BOOL CreateCompatibleBitmap(
    CDC* pDC,
    int nWidth,
    int nHeight
    );

    Then use
    CBitmap bitmap;
    bitmap.CreateCompatableBitmap(dc, widht, height);

    CDC memDC;
    memDC.SelectObject(bitmap);

    memDC.SetBkMode(TRANSPARENT);
    memDC.SetTextColor(RGB(0, 0, 0));
    memDC.SelectObject(&font);
    memDC.TextOut(0, 0, m_sCaption);

    CSize sz = memDC.GetTextExtent(m_sCaption);


    Now if you draw on the memDC, you will be drawing on the Bitmap

  3. #3
    Join Date
    Apr 2001
    Location
    USA, Knoxville, TN
    Posts
    255
    hey Jase,

    I've been collecting everythign useful related to bitmaps in a slightly expanded BDLG sample, it might be helpful.

    http://www.sayslaw.com/Thynx/C/bdlg/bdlg.htm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured