473,379 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Converting html page to image

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
5 7256
Hi Steven,

Try a google query with "convert html gif".

Succes,
Bart

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

Dec 12 '05 #2
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: SteveJ | last post by:
All, Can someone help me solve the next step. First of all let me say I'm new to php. I pieced the following code together from samples I found on the net and a book I bought called PHP...
9
by: Richard | last post by:
Considering the elemtary image swap routine in javascript: document.name.src="filename.gif" How would this be translated into PHP? So that if a visitor has js turned off, clicking on a...
3
by: lostinspace | last post by:
Hello, My sites were created primarily with tables as layout. :-( I have two page designs which I used frequently and hopefully somebody can provide some insight as to whether its best to remain...
1
by: Steven (remove wax and invalid for reply) | last post by:
Hello to all. I'm a member of this non-profit organization: http://www.hitagroup.org/ For several reasons, we need to move our web host and I got volunteered. The person who created it is no...
2
by: Ben Amada | last post by:
Hello, A partner is going to be creating some HTML files that I plan on converting to user controls (UC) and dynamically load at runtime. I'm guessing Visual Studio doesn't come with some...
6
by: Krakatioison | last post by:
I've seen once program which was able to convert any url to JPG file. Once I tried it on cnn.com website and it created the nice jpg, even that the page was scrollable, so it was not using...
5
by: Varangian | last post by:
Hi there experts, Is there a way to convert a System.Drawing.Image to an ImageButton. Maybe using ChangeType I don't know! Thank you
4
by: Beginner | last post by:
How do I convert JPEG images to binary files in ASP.NET? Please advice. Thanks.
3
by: Raghu Raman | last post by:
Hi i want to save the read the image file and show it on a image control of a mobile webform .But the mobile control does not support html images ,so i am forced to use the server image control...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.