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

How to write and read from streams (Istream) Save webpage as Image

P: n/a

Dear Friends

I am new guy to Visual C++.NET

I've program to save website as a image vc++.net .
It have a function "SaveSnapshot" to save the webpage as an image

On that function ifor saving as a image it uses image.Save method()

This image.Save function have two overloaded form's first one is file name
and the second

HRESULT image.Save(LPCTSTR pszFileName,REFGUID guidFileType = GUID_NULL)
HRESULT image.Save(IStream *pStream,REFGUID guidFileType)

The code's shown below uses the first method..

But I want the binary content of the image and not want to save in a
separate file
It means that I want to use the second method and want to change the
SaveSnapshot to return the ByteArray Of the image.
How to achive this ??

Is there any possible to convert the code to C#??

----------------------------------------------------
#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;
}
----------------------------------------------------------------------
Nov 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.