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.
Printable View
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.
Hello an urgent help is required for
copying CLientDC content to the Memory device context.
How to do this?
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);
}
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
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:
Use this insteadCode:CClientDC clientDC(::AfxGetMainWnd());
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.Code:CClientDC clientDC(this);
TDM
Hi TDM
I had applied coorectiuon mentioned:
Well dialog problem solved but Mainframe is still there
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);
}
}
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
Any pointer to help