int nScrollbarWide= GetSystemMetrics( SM_CXVSCROLL );
int nEdgeWide= GetSystemMetrics( SM_CXEDGE );
int nNonClientWide= nScrollbarWide+nEdgeWide;
int nCtlWide= nThumbnailWide+nNonClientWide;
int nCtlHigh= nThumbnailWide+nNonClientWide;
m_ctlBrowser.MoveWindow( 0,0, nCtlWide,nCtlHigh );
Next, we need to decide on a zoom percentage. I ended up with a 20% zoom (1/5th normal size), but here's my thinking on the subject:
#include <atlimage.h> // needed for CImage object
void DelayMs( int nMs );
void CThumbnailWebPgDlg::OnBnClickedButton1()
{
int nThumbnailWide= 200;
int nThumbnailHigh= 200;
int nZoomPercent= 21; // see text for calculations
//------------------ set the size of the browser control
int nScrollbarWide= GetSystemMetrics( SM_CXVSCROLL );
int nEdgeWide= GetSystemMetrics( SM_CXEDGE );
int nNonClientWide= nScrollbarWide+nEdgeWide;
int nCtlWide= nThumbnailWide+nNonClientWide;
int nCtlHigh= nThumbnailWide+nNonClientWide;
m_ctlBrowser.MoveWindow( 0,0, nCtlWide,nCtlHigh );
//--------------- trick to avoid timing problems (see text)
CString sUrl= L"about:blank";
m_ctlBrowser.Navigate( sUrl, 0,0,0,0 );
while ( m_ctlBrowser.get_ReadyState() != READYSTATE_COMPLETE ) {
DelayMs( 100 );
}
CComVariant vZoom( nZoomPercent );
m_ctlBrowser.ExecWB(
OLECMDID_OPTICAL_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER,
&vZoom, NULL);
// ---------------------------------------- now load the target page
sUrl= L"http://www.experts-exchange.com/Programming/Languages/CPP/";
m_ctlBrowser.Navigate( sUrl, 0,0,0,0 );
while ( m_ctlBrowser.get_ReadyState() != READYSTATE_COMPLETE ) {
DelayMs( 100 );
}
DelayMs( 3000 ); // extra delay to let flash, et al., show its image
//------------ Get the part of the image we want into a bitmap
//------------ Create a memory DC and bitmap to hold the image
CDC* pDC= GetDC();
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);
CBitmap cBmp;
cBmp.CreateCompatibleBitmap( pDC, nThumbnailWide,nThumbnailHigh );
CBitmap* pOldBitmap = dcMemory.SelectObject( &cBmp );
//------ copy image from the browser window to the memory DC
CDC* pBrowserDC= m_ctlBrowser.GetDC();
dcMemory.BitBlt(
0,0, // dest x,y
nThumbnailWide,nThumbnailHigh,
pBrowserDC,
nEdgeWide,nEdgeWide, // src x,y skips frame
SRCCOPY
);
dcMemory.SelectObject( pOldBitmap ); // standard cleanup
//-------------- output the thumbnail image to a file
//-------------- options are .JPG, .BMP and .PNG
CImage cImg;
cImg.Attach(cBmp);
cImg.Save( L"c:\\temp\\temp.jpg", Gdiplus::ImageFormatJPEG );
// cImg.Save( L"c:\\temp\\temp.bmp" );
// cImg.Save( L"c:\\temp\\temp.png", Gdiplus::ImageFormatPNG );
}
//------------- utility fn for waiting for the browser
void DelayMs( int nMs )
{
DWORD nMaxTick= GetTickCount()+ nMs;
while ( GetTickCount() < nMaxTick ) {
MSG msg;
while ( ( GetTickCount() < nMaxTick )
&& (::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) )
) {
AfxGetApp()->PumpMessage(); // does a ::GetMessage()
}
Sleep(1);
}
}
There are a few items that need some explanation:
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (5)
Commented:
Author
Commented:There might be an alternate way to obtain a single large JPG of a long page (say, 1000 x 20,000), possibly by using the WM_PRINT message, but that was not the focus of this article.
Commented:
Author
Commented:Commented: