我是vc ++的初学者。我试图将一个html文件转换为图像并将其保存为png,在vc ++中。现在我将html转换为位图并使用CImage的保存功能进行保存。我想在Windows 2008中运行此应用程序。它在手动运行应用程序时正常工作。我需要通过计划任务来运行它。但是,当应用程序通过计划任务运行时,不会创建图像。
我该如何克服这个问题?
这是我的代码
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);
img.Attach(m_hBmp);
if(!hr ==img.Save(szDestFilename))
return FALSE;
img.Detach();
img.Destroy();
pViewObject->Release();
return TRUE;
}