Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi;

here is my code to take a snapshot from static picture control (IDC_STATIC), and the image set into memory i can paste a copy to paint program, my question is how can i save this image to file (i am using VC 2008), please modify my code to do so,

thanks


the code
C++
CDC dc;

   /* client area only */
	CRect rect;
CWnd *wnd = GetDlgItem(IDC_STATIC);
wnd->GetWindowRect(&rect);
//wnd->ScreenToClient(&rect); //optional step - see below

         HDC hdc = ::GetDC(wnd->m_hWnd);
         dc.Attach(hdc);
       

     CDC memDC;

     memDC.CreateCompatibleDC(&dc);

     CBitmap bm;
     CRect r;

      
         wnd->GetClientRect(&r);
        

     CString s;
     wnd->GetWindowText(s);

     CSize sz(r.Width(), r.Height());

     bm.CreateCompatibleBitmap(&dc, sz.cx, sz.cy);

     CBitmap * oldbm = memDC.SelectObject(&bm);

     memDC.BitBlt(0, 0, sz.cx, sz.cy, &dc, 0, 0, SRCCOPY);

     wnd->GetParent()->OpenClipboard();
     ::EmptyClipboard();
     ::SetClipboardData(CF_BITMAP, bm.m_hObject);
     CloseClipboard();

     memDC.SelectObject(oldbm);
     bm.Detach(); 
Posted
Updated 21-Oct-14 1:01am
v2

1 solution

Using GDI+ makes it simple, see "SAVE CBITMAP TO FILE"[^].
 
Share this answer
 
Comments
aymanshebl 21-Oct-14 7:35am    
thank a lot :)
CPallini 21-Oct-14 7:48am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900