Getting image from wewbsite

J

Johnny Jörgensen

Does anybody know how you can extract an image fron a webpage loaded into a
webbrowser control and either save it to file OR save it in a database?

Cheers,
Johnny J.
 
M

Mythran

Johnny Jörgensen said:
Does anybody know how you can extract an image fron a webpage loaded into
a webbrowser control and either save it to file OR save it in a database?

Cheers,
Johnny J.

You need to read in the image as if you were reading in a webpage....first,
open the web page and parse the path to the image...then, you can use the
following code to get the byte-array of the image and store to a file or
data column in a database.

// BEGIN C#

string url =
@"http://www.co.merced.ca.us/CountyWeb/images/GeneralActive.gif";
HttpWebRequest request =
(HttpWebRequest) WebRequest.Create(url);
HttpWebResponse response =
(HttpWebResponse) request.GetResponse();

byte[] bytes;
using (Stream stream = response.GetResponseStream()) {
bytes = new byte[response.ContentLength];
stream.Read(bytes, 0, bytes.Length);
}

// Now we have a byte array to do as we wish, saving to a file
now.
FileStream fs = File.Create(@"C:\GeneralActive.gif");
fs.Write(bytes, 0, bytes.Length);
fs.Close();

// OR write to a database.
// To do this, create an IMAGE (SQL Server) column and just copy
// the byte array into the field for a DataRow. An Image column
// maps to a byte-array in a DataColumn in a DataRow.

// END C#

There may be an easier or better way in a more recent version of the .Net
Framework though... :)


HTH,
Mythran
 
H

Herfried K. Wagner [MVP]

Johnny Jörgensen said:
Does anybody know how you can extract an image fron a webpage loaded into
a webbrowser control and either save it to file OR save it in a database?

'My.Computer.Network.DownloadFile'
'System.Net.WebClient.DownloadFile'

You can store the binary data in the database as a BLOB.
 
J

Johnny Jörgensen

Thanks for the suggestions all of you.

Howevet, having had time to think about it, it seems like I'm missing
something here.

You see, as I wrote, I already have a webpage loaded in a webbrowser
containing the image I want to save to a database field.

So there should be no reason for downloading it AGAIN (using extra time to
do that). There MUST be a way to "pluck out" the image from the webbrowser
control in a form that's database saveable.

Cheers,
Johnny J.
 
C

Cor Ligthert[MVP]

Johnny,

There is no image loaded in your webbrowser, a webbrowser is just using the
IE part of the OS. In the pages there is an image tag to the url about we
all are talking about in a way. That URL you can find using MSHTML (Which is
nothing more then the representation of the DHTML object model often called
***the*** DOM).

Cor
 
J

Johnny Jörgensen

Cor, you're not making sense - are you trying to tell me that the image I
see in the Webbrowser rendering of the HTML is not really there? Have I
started imagining things?

I certainly don't see the URL directly in the webbrowser (but I do of course
see it using the HTML document property. But the Webbrowser is rendering and
showing the HTML elements, which means that the picture I see in the webpage
must be some sort of an Image object that should be possible to reference.

All the solutions I've been given are using the same appoach: To use the URL
in the HTML to download the file off the internet. But don't you see: The
file has already been downloaded once! Why should I download it again? It
ought to be in the Internet file cache?

Cheers,
Johnny J.
 
J

Joey Joe Joe

That's exactly where they are. They're in your Temporary Internet Files
folder.
 
L

Lloyd Sheen

Johnny Jörgensen said:
Cor, you're not making sense - are you trying to tell me that the image I
see in the Webbrowser rendering of the HTML is not really there? Have I
started imagining things?

I certainly don't see the URL directly in the webbrowser (but I do of
course see it using the HTML document property. But the Webbrowser is
rendering and showing the HTML elements, which means that the picture I
see in the webpage must be some sort of an Image object that should be
possible to reference.

All the solutions I've been given are using the same appoach: To use the
URL in the HTML to download the file off the internet. But don't you see:
The file has already been downloaded once! Why should I download it again?
It ought to be in the Internet file cache?

Cheers,
Johnny J.

Sorry but he totally correct. While the file is in your cache there is no
image in the document. The items in the document simply tell the browser
what to render. When it hits the img tag it gets the downloaded image from
the cache and displays it. As for extra time if you need to download the
file it should recognise that it exists already and use the item in the
cache.

LS
 
C

Cor Ligthert[MVP]

Joey Joe Joe said:
That's exactly where they are. They're in your Temporary Internet Files
folder.
Which you could find in W98 and IE4 direct in the C drive folder, however a
little bit changed.

In XP it has all kind of GUID like other names, in Vista I don't even see it
even anymore.

Beside that are images mostly very short in cache because that they have
mostly a retention time of 0.

Cor
 
J

Joey Joe Joe

That's because the folder is hidden, but it's still there. In Vista (unless
you move it):

C:\Users\.....\AppData\Local\Microsoft\Windows\Temporary Internet Files
 
L

Lloyd Sheen

Joey Joe Joe said:
That's because the folder is hidden, but it's still there. In Vista
(unless you move it):

C:\Users\.....\AppData\Local\Microsoft\Windows\Temporary Internet Files

Simplest way to find the temp files in Vista is to use Tools/Internet
Options/General Tab.

Click on Settings then View Files

LS
 
T

Trevor Benedict

Johnny,
Since the image is already cached and the solution provided is to download
it using the URL, when you send the request to the server, you may still end
up getting the file from the Cache. Except for the call to the server, you
might still be reusing the downloaded image.

Regards,

Trevor Benedict
MCSD
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top