473,322 Members | 1,409 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,322 software developers and data experts.

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


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
0 4096

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default...
6
by: Steve | last post by:
Hi, I'm trying to convert a file reading loop into one using streams. The BSD OS read API returns the number of bytes read, but istream::read returns itself. How can I find out the number of...
3
by: Rakesh Sinha | last post by:
I have a very trivial question. But I searched in google / archives of this group to get the answer, checked the C++ FAQ - but did not precisely what I was looking for. The problem is with...
8
by: bonj | last post by:
hello I hope somebody can help me get my head around this area of 'stream' programming... I know that streams are very fashionable nowadays so hopefully there'll be lots of replies. ;-) ...
0
by: CroDude | last post by:
Hi all! I have problems when writting bitmap to a byte array and after reading it back form byte to Bitmap object. What I do is this: First I throw Bitmap to a memory-stream and then I write it...
17
by: chris.schwalm | last post by:
I would like to first state that I have searched through the archives and found a lot of related material, but am still new enough to javascript that I can't fit all the pieces together yet. So...
0
by: Alfonso | last post by:
Hello , I have a problem writing and reading images (jpeg, bmp) from a my Web Service. I developed a client and a WebService, I host the webservice on IIS5.1 I send an image as a byte...
1
by: raj.sinha | last post by:
I have to "PUT" data to a Unicode file... a file that has the "FF FE" mark at the beginning of the file. How do i do that. What HTTP header do i need to send so that the data is stored in the...
6
by: Ioannis Papadopoulos | last post by:
I would like to extend the functionality of all streams in C++ so I can do some fancy stuff like redirecting the streams on the fly. I don't want to reimplement the whole stream support in C++...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.