BOOL CCreateHtml::CreateImage(IHTMLDocument2 *pDoc,LPCTSTR szDestFilename,CSize srcSize)
{
IHTMLDocument3* pDocument3 = NULL;
IHTMLDocument2* pDocument = NULL;
IHTMLElement2* pElement2 = NULL;
IHTMLElement* pElement = NULL;
IViewObject2* pViewObject = NULL;
IDispatch* pDispatch = NULL;
IViewObject* pViewObj = NULL;
HRESULT hr;
long bodyHeight;
long bodyWidth;
long rootHeight;
long rootWidth;
long height;
long width;
CImage img;
if(FAILED(m_pBrowser->get_Document(&pDispatch)))
return FALSE;
if(FAILED(pDispatch->QueryInterface(IID_IHTMLDocument2,(void**)&pDocument)))
return FALSE;
if(FAILED(pDocument->get_body(&pElement)))
return FALSE;
if(FAILED(pElement->QueryInterface(IID_IHTMLElement2,(void**)&pElement2)))
return FALSE;
if(FAILED(pElement2->get_scrollHeight(&bodyHeight)))
return FALSE;
if(FAILED(pElement2->get_scrollWidth(&bodyWidth)))
return FALSE;
if(FAILED(pDispatch->QueryInterface(IID_IHTMLDocument3,(void**)&pDocument3)))
return FALSE;
if(FAILED(pDocument3->get_documentElement(&pElement)))
return FALSE;
if(FAILED(pElement->QueryInterface(IID_IHTMLElement2,(void**)&pElement2)))
return FALSE;
if(FAILED(pElement2->get_scrollHeight(&rootHeight)))
return FALSE;
if(FAILED(pElement2->get_scrollWidth(&rootWidth)))
return FALSE;
HBITMAP m_hBmp;
width = bodyWidth;
height = rootHeight > bodyHeight ? rootHeight : bodyHeight;
if(width > 2000)
width = 2000;
if(height > 2000)
height = 2000;
MoveWindow(0,0,width,height,TRUE);
::MoveWindow(m_hwndWebBrowser,0,0,width,height,TRUE);
if(FAILED(m_pBrowser->QueryInterface(IID_IViewObject2,(void**)&pViewObject)))
return FALSE;
CDC *cdcMain = GetDC();
HDC hdcMain = *cdcMain;
HDC hdcMem = CreateCompatibleDC(hdcMain);
m_hBmp = CreateCompatibleBitmap(hdcMain,width,height);
SelectObject(hdcMem,m_hBmp);
RECTL rcBounds = { 0, 0, width, height };
hr = pViewObject->Draw(DVASPECT_CONTENT, -1, NULL, NULL,hdcMain,hdcMem, &rcBounds, NULL, NULL, 0);
//Here is the code for saving an image as png
CreateImage();
ILHANDLE devHandle = m_hBmp;
ilInit();
ilBindImage(iluGenImage());
ilLoadImage("123.bmp");
ilSaveImage("abc1.png");
pViewObject->Release();
return TRUE;
}
//This method is used to save a .bmp image as a bitmap stream
BOOL CCreateHtml::CreateImage()
{
BITMAP bmp;
HBITMAP hBmp = m_hBmp;
int n= ::GetObject( hBmp,sizeof( BITMAP ),&bmp );
BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bmp.bmWidth;
bi.biHeight = bmp.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
DWORD dwBmpSize = ((bmp.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmp.bmHeight;
HANDLE hDIB = GlobalAlloc(GHND,dwBmpSize);
char *lpbitmap = (char *)GlobalLock(hDIB);
int m = GetDIBits((HDC)GetDC(), hBmp, 0,
(UINT)bmp.bmHeight,
lpbitmap,
(BITMAPINFO *)&bi, DIB_RGB_COLORS);
CString str = "captureqwsx.bmp";
LPCSTR fNam =(LPCSTR)(LPCTSTR) str;
HANDLE hFile = CreateFile(fNam,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
//Offset to where the actual bitmap bits start.
bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
bmfHeader.bfSize = dwSizeofDIB;
bmfHeader.bfType = 0x4D42; //BM
DWORD dwBytesWritten = 0;
WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);
//Unlock and Free the DIB from the heap
GlobalUnlock(hDIB);
GlobalFree(hDIB);
//Close the handle for the file that was created
CloseHandle(hFile);
//Clean up done:
//DeleteObject(hBmp);
return TRUE;
}