C #. NET saves an image image to a local disk based on a URL link

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.