Nelson T Joseph
Nelson T Joseph

Reputation: 2763

How can I save a bitmap as a png in vc++ without using CImage

I am a beginner in vc++. I am trying to convert an html file to image and save it as png, in vc++. Now I converted the html into bitmap and save using save function of CImage. I want to run this application in windows 2008. It works properly when run the application manually. I need to run it through a schedule task. But when the application runs through schedule task the image is not created.

How can I overcome this problem?

This is my code

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;

}

Upvotes: 0

Views: 1225

Answers (1)

the_source
the_source

Reputation: 648

You're not gonna like the answer but there's is no build-in support for PNG images, you'll need to use an external library like pnglib

Good luck!

Upvotes: 1

Related Questions