Author Topic: Get and save the image returned by the Google Maps to a file.  (Read 1796 times)

0 Members and 1 Guest are viewing this topic.

FELIX

  • Bull Frog
  • Posts: 241
Get and save the image returned by the Google Maps to a file.
« on: October 27, 2018, 11:41:41 AM »
How to get and save the image returned by the string to a file.

If they put the string as an example in a browser it returns an image.

Code: [Select]
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"
« Last Edit: October 29, 2018, 02:20:25 PM by FELIX »
OK.

n.yuan

  • Bull Frog
  • Posts: 348
Re: Get and save the image returned by the Google Maps to a file.
« Reply #1 on: October 28, 2018, 08:24:20 AM »
You do web download (text string, files, stream...) with HttpWebRequest, or WebClient, or latest technology HttpClient. If you google each one of them, you could find lots of code samples for downloading.

If the size of downloading is not big, using WebClinet would be easiest, here is the code sample that download the image as you posted:

Code: [Select]
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

  • Bull Frog
  • Posts: 241
Re: Get and save the image returned by the Google Maps to a file.
« Reply #2 on: October 29, 2018, 02:28:26 PM »
Okay, I got it too.

Code: [Select]
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)

 Thankful.
OK.