Board index » cppbuilder » Getting image from web site

Getting image from web site


2007-03-16 03:53:35 PM
cppbuilder106
Hi All
I'm not really a C++ programmer, but I have dabbled with C++Builder 5 a bit.
I'm trying to grab an image from a web site.
I can open a browser using CppWebBrowser1->Navigate(URL); and this is fine.
Firstly, how do I know when the page has completed, and second, how do you
get the images from this page?
Sorry if this has been asked a thousand times, but my C++ knowledge is
somewhat lacking.
 
 

Re:Getting image from web site

Grumps wrote:
Quote
Hi All
I'm not really a C++ programmer, but I have dabbled with C++Builder 5
a bit. I'm trying to grab an image from a web site.
I can open a browser using CppWebBrowser1->Navigate(URL); and this is
fine. Firstly, how do I know when the page has completed, and second,
how do you get the images from this page?
Sorry if this has been asked a thousand times, but my C++ knowledge is
somewhat lacking.
Ok. Got the page completed issue sorted using the OnDocumentComplete event.
Just need to get the images from the page.
 

Re:Getting image from web site

Graeme wrote:
Quote
Just need to get the images from the page.
T(Cpp)WebBrowsr has a method to give you all the img's on that page.
From thereon you can get the url in the src="url".
Code below shows the amount of images and after that displays the
src url's. Only it does not. It does display the url for the first image
only.
Maybe you can get it to work properly. At the moment I have no time for it.
If you get it to work pleas post the code.
Hans.
void __fastcall TForm1::SpeedButton22Click(TObject *Sender)
{
AnsiString AllImages;
IHTMLDocument2 *HTMLDoc = NULL;
if(SUCCEEDED(CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&HTMLDoc)))
{
IHTMLElementCollection *pAll = NULL;
if(SUCCEEDED(HTMLDoc->get_images(&pAll))) // mshtml.h
{
long collSize;
pAll->get_length(&collSize);
IDispatch* id;
VARIANT one;
for( int idx = 0; idx < collSize; idx++)
{
one.vt = VT_UINT;
one.lVal = idx;
VARIANT two = {0};
pAll->item(one,two,&id);
IHTMLElement* pElem = NULL;
id->QueryInterface( IID_IHTMLElement, (LPVOID*)&pElem );
BSTR bstr = NULL;
pElem->toString(&bstr);
AllImages += bstr;
AllImages += "\n";
}
ShowMessage ( "AllObjects for Images\n\n" + AllImages ); // shows you amount
long count = -1;
long length = 0;
pAll->get_length( &length);
// ShowMessage ( "length: " + AnsiString ( length ) );
TVariant name = NULL;
TVariant index = count;
IDispatch *pDisp = NULL;
while ( ++ count < 5) // 5!!
{
OleVariant varIndex((int)count);
// index = count;
pDisp = NULL;
if(SUCCEEDED(pAll->item(name, varIndex, &pDisp)))
// if(SUCCEEDED(pAll->item(name, index, &pDisp)))
//if(SUCCEEDED(pAll->item("",count, &pDisp)))
{
// ShowMessage ( "SUCCEEDED(pAll->item(name, index, &pDisp))) " + IntToStr ( (int)pDisp) );
if ( pDisp )
{
IHTMLImgElement *ImgElement = NULL; // mshtml.h
pDisp->QueryInterface(IID_IHTMLImgElement, (LPVOID*)&ImgElement);
pDisp->Release();
if(ImgElement)
{
WideString src;
ImgElement->get_src ( &src );
ShowMessage ( src) ;
SysFreeString (src);
ImgElement->Release();
}
}
}
else
break;
}
pAll->Release();
}
HTMLDoc->Release();
}
}
 

{smallsort}

Re:Getting image from web site

Hans Galema wrote:
Quote
Graeme wrote:

>Just need to get the images from the page.

T(Cpp)WebBrowsr has a method to give you all the img's on that page.

From thereon you can get the url in the src="url".

Code below shows the amount of images and after that displays the
src url's. Only it does not. It does display the url for the first
image only.

Maybe you can get it to work properly. At the moment I have no time
for it. If you get it to work pleas post the code.

Hans.

void __fastcall TForm1::SpeedButton22Click(TObject *Sender)
{
AnsiString AllImages;

IHTMLDocument2 *HTMLDoc = NULL;
I've fallen at the first hurdle!
Where do I get IHTMLDocument2 from? Is it part of C++Builder5?
 

Re:Getting image from web site

Grumps wrote:
Quote
I've fallen at the first hurdle!
Where do I get IHTMLDocument2 from? Is it part of C++Builder5?
The comment should have given you the clue:
if(SUCCEEDED(HTMLDoc->get_images(&pAll))) // mshtml.h
Hans.
 

Re:Getting image from web site

Hans Galema wrote:
Quote
Grumps wrote:

>I've fallen at the first hurdle!
>Where do I get IHTMLDocument2 from? Is it part of C++Builder5?

The comment should have given you the clue:

if(SUCCEEDED(HTMLDoc->get_images(&pAll))) // mshtml.h
Oops! It does now. Thanks.
 

Re:Getting image from web site

"Hans Galema" < XXXX@XXXXX.COM >wrote in message
Quote
Graeme wrote:

>Just need to get the images from the page.

T(Cpp)WebBrowsr has a method to give you all the img's on that page.

From thereon you can get the url in the src="url".

Code below shows the amount of images and after that displays the
src url's. Only it does not. It does display the url for the first image
only.

Maybe you can get it to work properly. At the moment I have no time for
it.
If you get it to work pleas post the code.
I've looked at the code, but have absolutely no idea why it doesn't work.
I've since found another code snippet that gets an image. In fact, there is
only one image on the page I'm looking at, which makes it easy.