Retrieving Map Images from Virtual Earth Web Services

In my previous post on the Virtual Earth Web Services I showed how you can use the Imagery Service to get the uri of a map, based on the center, zoom, height, width etc.  What I didn’t cover is what you can do with this information. Clearly if you have an control that can accept the uri of an image then you might be able to just set the source property of the control.  However, if you want to do something more “intelligent” with the map, you will most likely have to download the image.  Here’s a simple snippet of code that will take the uri of a map image and return you an in-memory image that you can then manipulate.


private static Image DownloadImageFromUri(string uri)
{
    var mapRequest = WebRequest.Create(new System.Uri(uri));
    WebResponse response = mapRequest.GetResponse();
    MemoryStream responseStream = GetWebUriStream(response);
    return new Bitmap(responseStream);
}


Be warned though: This code downloads the image synchronously – this means that if you execute this code on the UI thread your application will stop responding until the image has downloaded completely.  Following my previous comments on how slow the Imagery service appears to be, I would suggest doing all your Virtual Earth calls on a background thread, simply raising events when the UI needs to be repainted to display new map images.

Leave a comment