C # download Web images

Source: Internet
Author: User

It's old stuff, too.

Recently used

Write it down so you don't forget it later.

To download a picture, first you have a picture address

To have a picture address, you must first put down the page to analyze the URL

Download Web page generally in two ways

1, with System.Net.WebClient

Using system.net;using system.windows.forms;string url = "http://www.cnblogs.com"; string result = Null;try{    WebClient client = new WebClient ();    result = client. downloadstring (URL);} catch (Exception ex) {    MessageBox.Show (ex. Message);}

2, with System.Net.HttpWebRequest

Using system.net;using system.io;using system.windows.forms;string result = null;string url = "Http://www.cnblogs.com"; WebResponse response = null; StreamReader reader = null;try{    HttpWebRequest request = (HttpWebRequest) webrequest.create (URL);    Request. Method = "GET";    Response = Request. GetResponse ();    reader = new StreamReader (response. GetResponseStream (), Encoding.UTF8);    result = reader. ReadToEnd ();} catch (Exception ex) {    MessageBox.Show (ex. Message);} finally{    if (reader! = null)        reader. Close ();    if (response! = NULL)        response. Close ();}

As for how to find the image URL skip, just say download Picture it

In fact, the same as above, there are two ways:

1,webrequest and WebResponse

WebRequest request = WebRequest.Create ("Http://images.cnblogs.com/logo_small.gif"); WebResponse response = Request. GetResponse (); Stream reader = response. GetResponseStream (); FileStream writer = new FileStream ("X:\\pic.jpg", FileMode.OpenOrCreate, FileAccess.Write); byte[] buff = new byte[512]; int c = 0; The number of bytes actually read while (C=reader. Read (buff, 0, buff. Length)) > 0) {    writer. Write (buff, 0, c);} Writer. Close (); writer. Dispose (); reader. Close (); reader. Dispose (); response. Close ();

2,webclient

String url = "Http://images.cnblogs.com/logo_small.gif"; string filepath = "X:\\pic.jpg"; WebClient mywebclient = new WebClient (); mywebclient. DownloadFile (URL, filepath);

C # download Web images

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.