How to save the client area of ​​a child window to a bitmap file?

I created a Windows application using basic WIN32 and VC ++. In my parent window, I have a child window and two save and send buttons.

When the user clicks the Save button, I want savefileDialog be open and the user should save the image as a raster file.

The same file should be sent to the remote user using the WinSock API .... My problem is that I do not know how to save the screenshot in a raster file ...

Please help me with this ... I have not used MFC, ATL or WTL ....

thanks in advance,

+7
source share
1 answer
 RECT rect = {0}; GetWindowRect( hwnd, &rect ); ATL::CImage* image_ = new CImage(); image_ -> Create( rect.right - rect.left, rect.bottom - rect.top, 32 ); HDC device_context_handle = image_ -> GetDC(); PrintWindow( hwnd, device_context_handle, PW_CLIENTONLY ); image_ -> Save( filename ); image_ -> ReleaseDC(); delete image_; 

PrintWindow() should do the trick.

Save as HBITMAP:

 HDC hDC = GetDC( hwnd ); HDC hTargetDC = CreateCompatibleDC( hDC ); RECT rect = {0}; GetWindowRect( hwnd, &rect ); HBITMAP hBitmap = CreateCompatibleBitmap( hDC, rect.right - rect.left, rect.bottom - rect.top ); SelectObject( hTargetDC, hBitmap ); PrintWindow( hwnd, hTargetDC, PW_CLIENTONLY ); SaveBMPFile( filename, hBitmap, hTargetDC, rect.right - rect.left, rect.bottom - rect.top ); DeleteObject( hBitmap ); ReleaseDC( hwnd, hDC ); DeleteDC( hTargetDC ); 

I will leave the implementation of SaveBMPFile to you; )

+11
source

All Articles