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