By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,866 Members | 1,765 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,866 IT Pros & Developers. It's quick & easy.

Converting html page to image

P: n/a
Hello All!

Has C# any classes to convert html page to gif or tif?
Anyone could please, give how to do it?

Br

Wew
Dec 11 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Steven,

Try a google query with "convert html gif".

Succes,
Bart

http://www.xenopz.com/blog/bartdeboeck

Dec 12 '05 #2

P: n/a
Essentially what you are describing is taking a screen capture of the web
page in the browser window.
There is at least one project on gotdotnet.com that illustrates the coding
involved.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Steven Hill" wrote:
Hello All!

Has C# any classes to convert html page to gif or tif?
Anyone could please, give how to do it?

Br

Wew

Dec 12 '05 #3

P: n/a
Too much small software when searching from Google. Not enought good
tutorials or samples around how to program itself.

<ba**********@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi Steven,

Try a google query with "convert html gif".

Succes,
Bart

http://www.xenopz.com/blog/bartdeboeck

Dec 12 '05 #4

P: n/a
here is c++ code... see if u can translate it.
// iecapt.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

////////////////////////////////////////////////////////////////////
//
// IECapt - A Internet Explorer Web Page Rendering Capture Utility
//
// Copyright (C) 2003 Bjoern Hoehrmann <bj****@hoehrmann.de>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// $Id: IECapt.cpp,v 1.2 2005/02/11 14:23:03 hoehrmann Exp $
//
////////////////////////////////////////////////////////////////////

#define VC_EXTRALEAN
#include <stdlib.h>
#include <windows.h>
#include <mshtml.h>
#include <exdispid.h>
#include <atlbase.h>
#include <atlwin.h>
#include <atlcom.h>
#include <atlhost.h>
#include <atlimage.h>
#undef VC_EXTRALEAN

class CMain;
class CEventSink;

//////////////////////////////////////////////////////////////////
// CEventSink
//////////////////////////////////////////////////////////////////
class CEventSink :
public CComObjectRootEx <CComSingleThreadModel>,
public IDispatch
{
public:
CEventSink() : m_pMain(NULL) {}

public:

BEGIN_COM_MAP(CEventSink)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY_IID(DIID_DWebBrowserEvents2, IDispatch)
END_COM_MAP()

STDMETHOD(GetTypeInfoCount)(UINT* pctinfo);
STDMETHOD(GetTypeInfo)(UINT itinfo, LCID lcid, ITypeInfo**
pptinfo);
STDMETHOD(GetIDsOfNames)(REFIID riid, LPOLESTR* rgszNames, UINT
cNames, LCID lcid, DISPID* rgdispid);
STDMETHOD(Invoke)(DISPID dispid, REFIID riid, LCID lcid, WORD
wFlags, DISPPARAMS* pdispparams,
VARIANT* pvarResult, EXCEPINFO* pexcepinfo, UINT*
puArgErr);

public:
CMain* m_pMain;
};

//////////////////////////////////////////////////////////////////
// CMain
//////////////////////////////////////////////////////////////////
class CMain :
public CWindowImpl <CMain>
{
public:

CMain() : m_dwCookie(0) { }

public:

BEGIN_MSG_MAP(CMainWindow)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
MESSAGE_HANDLER(WM_SIZE, OnSize)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
END_MSG_MAP()

public:

LRESULT OnCreate (UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL&
bHandled);
LRESULT OnSize (UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL&
bHandled);
LRESULT OnDestroy (UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL&
bHandled);

public:

BOOL SaveSnapshot(IDispatch* pdisp, VARIANT* purl);

public:
LPCSTR m_URI;
LPCSTR m_fileName;

protected:
CComPtr<IUnknown> m_pWebBrowserUnk;
CComPtr<IWebBrowser2> m_pWebBrowser;
CComObject<CEventSink>* m_pEventSink;
HWND m_hwndWebBrowser;
DWORD m_dwCookie;
};

//////////////////////////////////////////////////////////////////
// Implementation of CEventSink
//////////////////////////////////////////////////////////////////
STDMETHODIMP CEventSink::GetTypeInfoCount(UINT* pctinfo)
{
return E_NOTIMPL;
}

STDMETHODIMP CEventSink::GetTypeInfo(UINT itinfo, LCID lcid,
ITypeInfo** pptinfo)
{
return E_NOTIMPL;
}

STDMETHODIMP CEventSink::GetIDsOfNames(REFIID riid, LPOLESTR*
rgszNames,
UINT cNames, LCID lcid,
DISPID* rgdispid)
{
return E_NOTIMPL;
}

STDMETHODIMP CEventSink::Invoke(DISPID dispid, REFIID riid, LCID lcid,
WORD wFlags, DISPPARAMS* pdispparams,
VARIANT* pvarResult, EXCEPINFO*
pexcepinfo,
UINT* puArgErr)
{
if (dispid != DISPID_DOCUMENTCOMPLETE)
return S_OK;

if (pdispparams->cArgs != 2)
return S_OK;

if (pdispparams->rgvarg[0].vt != (VT_VARIANT | VT_BYREF))
return S_OK;

if (pdispparams->rgvarg[1].vt != VT_DISPATCH)
return S_OK;

if (m_pMain->SaveSnapshot(pdispparams->rgvarg[1].pdispVal,
pdispparams->rgvarg[0].pvarVal))
m_pMain->PostMessage(WM_CLOSE);

return S_OK;
}

//////////////////////////////////////////////////////////////////
// Implementation of CMain Messages
//////////////////////////////////////////////////////////////////
LRESULT CMain::OnCreate(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL&
bHandled)
{
HRESULT hr;
RECT old;
IUnknown * pUnk = NULL;
GetClientRect(&old);

m_hwndWebBrowser = ::CreateWindow(_T(ATLAXWIN_CLASS), m_URI,
/*WS_POPUP|*/WS_CHILD|WS_DISABLED, old.top, old.left,
old.right,
old.bottom, m_hWnd, NULL, ::GetModuleHandle(NULL), NULL);

hr = AtlAxGetControl(m_hwndWebBrowser, &m_pWebBrowserUnk);

if (FAILED(hr))
return 1;

if (m_pWebBrowserUnk == NULL)
return 1;

hr = m_pWebBrowserUnk->QueryInterface(IID_IWebBrowser2,
(void**)&m_pWebBrowser);

if (FAILED(hr))
return 1;

hr = CComObject<CEventSink>::CreateInstance(&m_pEventSi nk);

if (FAILED(hr))
return 1;

m_pEventSink->m_pMain = this;

hr = AtlAdvise(m_pWebBrowserUnk, m_pEventSink->GetUnknown(),
DIID_DWebBrowserEvents2, &m_dwCookie);

if (FAILED(hr))
return 1;

return 0;
}
LRESULT CMain::OnSize(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL&
bHandled)
{
if (m_hwndWebBrowser != NULL)
::MoveWindow(m_hwndWebBrowser, 0, 0, LOWORD(lParam),
HIWORD(lParam), TRUE);

return 0;
}

LRESULT CMain::OnDestroy(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL&
bHandled)
{
HRESULT hr;

if (m_dwCookie != 0)
hr = AtlUnadvise(m_pWebBrowserUnk, DIID_DWebBrowserEvents2,
m_dwCookie);

m_pWebBrowser.Release();
m_pWebBrowserUnk.Release();

PostQuitMessage(0);

return 0;
}

//////////////////////////////////////////////////////////////////
// Implementation of CMain::SaveSnapshot
//////////////////////////////////////////////////////////////////
BOOL CMain::SaveSnapshot(IDispatch* pdisp, VARIANT* purl)
{
IHTMLDocument3* pDocument3 = NULL;
IHTMLDocument2* pDocument = NULL;
IHTMLElement2* pElement2 = NULL;
IHTMLElement* pElement = NULL;
IViewObject2* pViewObject = NULL;
IDispatch* pDispatch = NULL;
IDispatch* pWebBrowserDisp = NULL;

HRESULT hr;
long bodyHeight;
long bodyWidth;
long rootHeight;
long rootWidth;
long height;
long width;

hr = m_pWebBrowser->get_Document(&pDispatch);

if (FAILED(hr))
return true;

hr = m_pWebBrowserUnk->QueryInterface(IID_IDispatch,
(void**)&pWebBrowserDisp);

if (FAILED(hr))
return true;

if (pWebBrowserDisp != pdisp)
{
pWebBrowserDisp->Release();
return false;
}

hr = pDispatch->QueryInterface(IID_IHTMLDocument2,
(void**)&pDocument);

if (FAILED(hr))
return true;

hr = pDocument->get_body(&pElement);

if (FAILED(hr))
return true;

hr = pElement->QueryInterface(IID_IHTMLElement2,
(void**)&pElement2);

if (FAILED(hr))
return true;

hr = pElement2->get_scrollHeight(&bodyHeight);

if (FAILED(hr))
return true;

hr = pElement2->get_scrollWidth(&bodyWidth);

if (FAILED(hr))
return true;

hr = pDispatch->QueryInterface(IID_IHTMLDocument3,
(void**)&pDocument3);

if (FAILED(hr))
return true;

hr = pDocument3->get_documentElement(&pElement);

if (FAILED(hr))
return true;

hr = pElement->QueryInterface(IID_IHTMLElement2,
(void**)&pElement2);

if (FAILED(hr))
return true;

hr = pElement2->get_scrollHeight(&rootHeight);

if (FAILED(hr))
return true;

hr = pElement2->get_scrollWidth(&rootWidth);

if (FAILED(hr))
return true;

width = bodyWidth;
height = rootHeight > bodyHeight ? rootHeight : bodyHeight;

MoveWindow(0, 0, width, height, TRUE);
::MoveWindow(m_hwndWebBrowser, 0, 0, width, height, TRUE);

hr = m_pWebBrowser->QueryInterface(IID_IViewObject2,
(void**)&pViewObject);

if (FAILED(hr))
return true;

HDC hdcMain = GetDC();
HDC hdcMem = CreateCompatibleDC(hdcMain);
HBITMAP hBitmap = CreateCompatibleBitmap(hdcMain, width,
height);
SelectObject(hdcMem, hBitmap);

RECTL rcBounds = { 0, 0, width, height };
hr = pViewObject->Draw(DVASPECT_CONTENT, -1, NULL, NULL,
hdcMain,
hdcMem, &rcBounds, NULL, NULL, 0);

if (SUCCEEDED(hr))
{
CImage image;
image.Create(width, height, 24);
CImageDC imageDC(image);
::BitBlt(imageDC, 0, 0, width, height, hdcMem, 0, 0,
SRCCOPY);
image.Save(m_fileName);
}

pViewObject->Release();
pWebBrowserDisp->Release();

return true;
}

static const GUID myGUID = { 0x445c10c2, 0xa6d4, 0x40a9, { 0x9c, 0x3f,
0x4e, 0x90, 0x42, 0x1d, 0x7e, 0x83 } };
static CComModule _Main;

int main (int argc, char *argv[])
{
//if (argc != 3)
//{
// printf("Usage: %s http://www.example.org/ localfile.png\n",
argv[0]);
// return EXIT_FAILURE;
//}

HRESULT hr = _Main.Init(NULL, ::GetModuleHandle(NULL), &myGUID);

if (FAILED(hr))
return EXIT_FAILURE;

if (!AtlAxWinInit())
return EXIT_FAILURE;

CMain MainWnd;

MainWnd.m_URI = argv[1];
MainWnd.m_fileName = argv[2];
RECT rcMain = { 0, 0, 800, 600 };
MainWnd.Create(NULL, rcMain, _T("IECapt"), WS_POPUP);

MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

_Main.Term();

return EXIT_SUCCESS;
}

Dec 17 '05 #5

P: n/a
The IViewObject doesn't exist in C# when you import the OLE dll, nor
have I seen an example of it's implementation in C#.

I would recommend using the IHTMLElementRender interface from
MSHTML.DLL and call it's DrawtoDC function. It seems to work 99% of the
time....just some odd CSS styles mess it up (Alpha blending for DIV's
for instance, I can't seem to get to appear.)

Dec 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.