Getting image from wewbsite

// 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#

你可能感兴趣的:(Getting image from wewbsite)