CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2003
    Location
    Jodhpur -> Rajasthan -> INDIA
    Posts
    453

    How to copy ClientDC to MemDC?

    Hi

    I am plotting on the ClientDC.
    Now at the end of function i want to copy the content of the ClientDc to MemDC, so that it can be used for saving image as BMP or JPG.
    Leave Your Mark Wherever You Go
    http://www.danasoft.com/sig/d0153030Sig.jpg

  2. #2
    Join Date
    May 2003
    Location
    Jodhpur -> Rajasthan -> INDIA
    Posts
    453

    Urgent-- Help required

    Hello an urgent help is required for

    copying CLientDC content to the Memory device context.

    How to do this?
    Leave Your Mark Wherever You Go
    http://www.danasoft.com/sig/d0153030Sig.jpg

  3. #3
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    Here are 2 functions that are useful. DumpClient copys the client contents to a member Bitmap and RestoreClient takes the copies the member bitmap (m_ClientBmp) back to the client. The contents of the client are then stored in a bitmap and not the memory dc itself. I think this approach is a better because there are a limited number of device contexts available at any given time.

    Code:
    void CGraphWnd::DumpClient()
    {
       CClientDC dc(this);
       CRect rcRect;
       GetClientRect(&rcRect);
    
       m_ClientBmp.DeleteObject();
       CBitmap *OldBmp;
       CDC memdc;
       memdc.CreateCompatibleDC(&dc); 
       m_ClientBmp.CreateCompatibleBitmap(&dc,rcRect.Width(),rcRect.Height());  
       OldBmp=memdc.SelectObject(&m_ClientBmp);
       memdc.BitBlt(0,0,rcRect.Width(),rcRect.Height(),&dc,0,0,SRCCOPY);
    
       memdc.SelectObject(OldBmp);
    }
    
    void CGraphWnd::RestoreClient()
    {
       CClientDC dc(this);
       CRect rcRect;
       GetClientRect(&rcRect);
    
       CBitmap *OldBmp;
       CDC memdc;
       memdc.CreateCompatibleDC(&dc); 
       OldBmp=memdc.SelectObject(&m_ClientBmp);
       dc.BitBlt(0,0,rcRect.Width(),rcRect.Height(),&memdc,0,0,SRCCOPY);
    
       memdc.SelectObject(OldBmp);
    }

  4. #4
    Join Date
    May 2003
    Location
    Jodhpur -> Rajasthan -> INDIA
    Posts
    453
    Hello TDM

    Please check the post at following thread. I am facing a serious problem. Might be bug/mistake from my side. I am not able to fix it. Hope you will help me out.


    http://www.codeguru.com/forum/showth...hreadid=296770
    Leave Your Mark Wherever You Go
    http://www.danasoft.com/sig/d0153030Sig.jpg

  5. #5
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    I had a similar problem. I have a progragram where the user can dump the client to a BMP file. But it would capture the file dialog. So what I did was basically do the DoModal for the file dialog then redraw the entire client then do the bit block transfer from the client to the memdc. So in your code on the other post in the SaveDCtoBMP function.


    Code:
     int ret = dlgFile.DoModal(); 
    /////redraw the client here//////
    if(IDOK == ret)
    {
    .......etc.......

    How you redraw the client will depend on how you have written your program. It might just be Invalidate or whatever other methods you have.


    Wait a minute.....hold on here.....you're getting the entire screen (mainframe and all). OK I see how to fix that.

    In SaveDCtoBMP you are using:

    Code:
    CClientDC clientDC(::AfxGetMainWnd());
    Use this instead

    Code:
    CClientDC clientDC(this);
    That way you get a DC for the view only, not the the entire mainframe. Using that along with redrawing the client after DoModal for the file dialog should give you what you need.

    TDM

  6. #6
    Join Date
    May 2003
    Location
    Jodhpur -> Rajasthan -> INDIA
    Posts
    453
    Hi TDM


    I had applied coorectiuon mentioned:

    Well dialog problem solved but Mainframe is still there
    Leave Your Mark Wherever You Go
    http://www.danasoft.com/sig/d0153030Sig.jpg

  7. #7
    Join Date
    May 2003
    Location
    Jodhpur -> Rajasthan -> INDIA
    Posts
    453
    Hello

    even i use following code then also same problem persists, which states that the ClientDC to Memdc has passed this Mainframe portion

    Again i am just stretching the memdc content on the onDraw function and it displays the mainframe portion not the ClientDC what is required.


    Code:
    void CMyView::SaveDCtoBMP()
    {
      CString Filename;
      CFileDialog dlgFile (false,"*.*",NULL, OFN_HIDEREADONLY,"BMP Files: (*.bmp)|*.bmp||");
      dlgFile.m_ofn.lpstrTitle  = "Save As Image File";
    	
      int ret = dlgFile.DoModal(); 
      Invalidate();
      if(IDOK == ret)
      {
          Filename = dlgFile.GetFileName ();
          CDIBSectionLite	dib;
           HBITMAP hnd = (HBITMAP)m_WfrBmp.GetSafeHandle (); 	
           dib.SetBitmap (hnd);
           dib.Save(Filename);	
      }
    }
    Leave Your Mark Wherever You Go
    http://www.danasoft.com/sig/d0153030Sig.jpg

  8. #8
    Join Date
    May 2003
    Location
    Jodhpur -> Rajasthan -> INDIA
    Posts
    453

    Contd.

    Code:
    // On Draw--> draw the grey box on the view
    void CSampleView::OnDraw(CDC* pDC)
    {
    	CSampleDoc* pDoc = GetDocument();
    	ASSERT_VALID(pDoc);
    	
    	CRect rect(10,10,200,200);
    	CBrush br;
    	br.CreateSolidBrush (RGB(100,100,100));
    	CBrush *old = pDC->SelectObject (&br);
    
    	//pDC->Rectangle (10,10,200,200);
    	pDC->Rectangle (&rect);
    	pDC->FillRect (&rect,&br);
    	pDC->SelectObject (old);
    	br.DeleteObject ();
    }
    SAve the Content drawn on te view to the MemDC and then save the bitmap to the BMP file 
    
    void CSampleView::OnFileSave() 
    {
    	CBitmap bmp;
    	CDC MemDC;
    	bmp.CreateCompatibleBitmap (GetDC(),300,300);
    	MemDC.CreateCompatibleDC (GetDC());
    	CBitmap *old = MemDC.SelectObject (&bmp);
    	//GetDC()->BitBlt(0,0,300,300,&MemDC,0,0,SRCCOPY);
    	MemDC.BitBlt (0,0,300,300,GetDC(),0,0,SRCCOPY);
    	HBITMAP hnd = (HBITMAP)bmp.GetSafeHandle ();
    	CDIBSectionLite lt;
    	lt.SetBitmap (hnd);
    	lt.Save ("D:\\123.bmp");
    	MemDC.SelectObject (old);
    	bmp.DeleteObject ();
    }

    This results in teh 300*300 pixel big black bitmap file


    What i want is to save the drawing i.e. grey box to be saved to the bitmap file?

    How to do this i.e.

    Draw the content to the View
    OnSave--> Save to the local/global MemDC and
    Save the content to the Image file
    Leave Your Mark Wherever You Go
    http://www.danasoft.com/sig/d0153030Sig.jpg

  9. #9
    Join Date
    May 2003
    Location
    Jodhpur -> Rajasthan -> INDIA
    Posts
    453
    Any pointer to help
    Leave Your Mark Wherever You Go
    http://www.danasoft.com/sig/d0153030Sig.jpg

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