-
May 29th, 2004, 09:10 AM
#1
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.
-
May 29th, 2004, 11:42 PM
#2
Urgent-- Help required
Hello an urgent help is required for
copying CLientDC content to the Memory device context.
How to do this?
-
May 30th, 2004, 09:56 AM
#3
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);
}
-
May 31st, 2004, 09:48 AM
#4
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
-
May 31st, 2004, 10:07 AM
#5
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
-
May 31st, 2004, 10:26 AM
#6
Hi TDM
I had applied coorectiuon mentioned:
Well dialog problem solved but Mainframe is still there
-
May 31st, 2004, 10:46 AM
#7
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);
}
}
-
June 8th, 2004, 08:16 AM
#8
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
-
June 9th, 2004, 02:09 AM
#9
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|