472,801 Members | 1,155 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,801 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 4058

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++...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.