Original: C #. NET Save image Image to local disk based on URL link
Depending on the URL link of an image, you can display a picture in the browser, if you want to save the picture in the local disk by code in the following way:
1. First get the binary array of the picture.
static public byte[] Getbytesfromurl (string url)
{
Byte[] B;
HttpWebRequest Myreq = (HttpWebRequest) webrequest.create (URL);
WebResponse myresp = Myreq.getresponse ();
Stream stream = Myresp.getresponsestream ();
int i;
using (BinaryReader br = new BinaryReader (stream))
{
i = (int) (stream. Length);
b = Br. Readbytes (500000);
Br. Close ();
}
Myresp.close ();
return b;
}
2. Save to the disk file.
static public void Writebytestofile (String fileName, byte[] content)
{
FileStream fs = new FileStream (FileName, FileMode.Create);
BinaryWriter w = new BinaryWriter (FS);
Try
{
W.write (content);
}
Finally
{
Fs. Close ();
W.close ();
}
}
C #. NET saves an image image to a local disk based on a URL link