FELIX 发表于 2018-10-27 11:41:41

获取Google地图返回的图像并将其保存到文件中。

如何获取字符串返回的图像并将其保存到文件中
如果他们将字符串作为示例放在浏览器中,它将返回一个图像。
MYSTR ="https://maps.googleapis.com/maps/api/staticmap?center=-22.87392394,-42.44314953&size=512x512&scale=1&zoom=20&format=jpg&maptype=satellite&key=MYKEY"

**** Hidden Message *****

n.yuan 发表于 2018-10-28 08:24:20

您可以使用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

FELIX 发表于 2018-10-29 14:28:26

好的,我也知道了。
MYSTR ="https://maps.googleapis.com/maps/api/staticmap?center=-22.87392394,-42.44314953&size=512x512&scale=1&zoom=20&format=jpg&maptype=satellite&key=MYKEY"
Dim client As New WebClient()
client.DownloadFile(MYSTR, MYFILEIMG)
感谢。
页: [1]
查看完整版本: 获取Google地图返回的图像并将其保存到文件中。