Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save the client area of a child Window to a Bitmap file?

I have created a windows application using core WIN32 and VC++. In my parent window I have a child window and two buttons "save" and "send".

When user clicks the "save" button I want the savefileDialog to be opened and user should be able to save the image as a bitmap file.

The same file should be sent to a remote user using WinSock API.... My problem is, I don't know how to save the screen shot of the window to a bitmap file...

please help me out of this ... I have not used MFC, ATL or WTL....

thanks in advance,

like image 994
Amit Avatar asked May 10 '11 12:05

Amit


1 Answers

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.

To 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 up to you ; )

like image 87
Mike Kwan Avatar answered Oct 14 '22 11:10

Mike Kwan