您可以使用HttpWebRequest、WebClient或最新技术的HttpClient进行web下载(文本字符串、文件、流…)。如果你用谷歌搜索其中的每一个,你可以找到很多可供下载的代码示例
如果下载量不大,使用WebLinet将是最简单的,下面是下载您发布的图像的代码示例:
- using System;
- using System.Drawing;
- using System.IO;
- using System.Net;
- namespace DownloadFromTheNet
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Press Enter key to continue, Q to quit...");
- var pressed = Console.ReadLine();
- if (!pressed.ToUpper().StartsWith("Q"))
- {
- DoDownload();
- }
- }
- private static void DoDownload()
- {
- var imageUrl =
- "https://maps.googleapis.com/maps/api/staticmap?center=-22.87392394,-42.44314953&size=512x512&scale=1&zoom=20&format=jpg&maptype=satellite&key=AIzaSyAwGppNDUVLYkcHP6_8gUrbzHlNyu4B6NU";
- var filename = @"C:\Temp\MyDownloadPicture.jpg";
- using (var client = new WebClient())
- {
- Stream stream = client.OpenRead(imageUrl);
- var bitmap = new Bitmap(stream);
- if (bitmap != null)
- {
- if (File.Exists(filename)) File.Delete(filename);
- bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
- }
- stream.Flush();
- stream.Close();
- }
- }
- }
- }
HTH
|