Re: How to download web page with image?
- From: sasha <abc@xxxxxxx>
- Date: Thu, 08 Feb 2007 12:13:25 -0800
ddeng722@xxxxxxxxx wrote:
Hello, I'm developing web robot that crawl web page.
bye the way, one thing driving me crazy is to find out how to download
web page with image like 'save as' of explorer.
when we click the menu named 'save as' of explorer, html and image
files might be downloaded in the folder.
so actually, if you know about component or COM or kinda solution, let
me know.
and ... I'm programming on '.NET' with 'c#'.
Thank you for reading this.
Here is a C++ Builder code that I use in my utility that downloads images from the Astronomy Picture of the day site for me.
The 'wb' variable is the IE ActiveX control that I host on my form.
Hope it helps:
void TForm1::GetImage()
{
if ( !oldFile.IsEmpty() )
DeleteFile( oldFile );
oldFile.SetLength( 0 );
IHTMLDocument2Ptr &doc = wb;
if ( doc ) {
// holds file name
String name;
try {
IDispatchPtr disp;
TVariant idx( 0 );
doc->images->item( idx, TNoParam(), &disp );
if ( disp ) {
IHTMLImgElementPtr img = disp;
if ( img ) {
IHTMLElementPtr parent = disp;
IHTMLAnchorElementPtr link = parent->parentElement;
if ( link ) {
char path [MAX_PATH];
char file [MAX_PATH];
GetTempPath( sizeof( path ), path );
if ( GetTempFileName( path, "img", 0, file ) ) {
name = file;
name += ".jpg";
// delete the temp file windows created for us
DeleteFile( file );
// get our image file
URLDownloadToFile( 0, AnsiString( link->href ).c_str(), name.c_str(), 0, 0 );
memo->Lines->Add( link->href );
std::auto_ptr <TJPEGImage> jpg(new TJPEGImage);
jpg->LoadFromFile( name );
// no need to keep the downloaded image around
DeleteFile( name );
name.SetLength( 0 );
name = file;
name += ".bmp";
std::auto_ptr <Graphics::TBitmap> bitmap( new Graphics::TBitmap );
if ( jpg->Width > Screen->Width && jpg->Height > Screen->Height ) {
bitmap->Width = Screen->Width;
bitmap->Height = Screen->Height;
TRect r( 0, 0, Screen->Width, Screen->Height );
bitmap->Canvas->StretchDraw( r, jpg.get() );
} else
bitmap->Assign( jpg.get() );
bitmap->SaveToFile( name );
oldFile = name;
if ( !SystemParametersInfo( SPI_SETDESKWALLPAPER, 0,
name.c_str(), SPIF_UPDATEINIFILE ) ) {
memo->Lines->Add( String( "Error is " ) + GetLastError() );
} else {
tray->BalloonHint = String( "Got new image - " ) + last.CurrentDate() +
String( ". Next update at " ) + TDateTime( last + 1 ).DateTimeString();
tray->Hint = String( "Next update at " ) + (last + 1 + TDateTime::CurrentTime() ).DateTimeString();
tray->ShowBalloonHint();
}
}
}
}
}
}
catch( ... ) {
if ( !name.IsEmpty() )
DeleteFile( name );
}
}
}
.
- References:
- How to download web page with image?
- From: ddeng722
- How to download web page with image?
- Prev by Date: How to download web page with image?
- Next by Date: Re: BEFORENAVIGATE2 doesn't fire in IE7
- Previous by thread: How to download web page with image?
- Index(es):
Relevant Pages
|
|