Thursday, November 13, 2008

Download and Save Google Static Map Image Programatically

//Download Image
Uri uri = new Uri("http://maps.google.com/staticmap?center=45.728220,4.830321&zoom=8&size=200x200&maptype=roadmap&key=ABQIAAAAaHAby4XeLCIadFkAUW4vmRSkJGe9mG57rOapogjk9M-sm4TzXxR2I7bi2Qkj-opZe16CdmDs7_dNrQ");

HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(uri);

httpRequest.Timeout = 15000;

httpRequest.ReadWriteTimeout = 20000;

HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();

Stream imageStream = httpResponse.GetResponseStream();

//Bitmap MyImage= new Bitmap(imageStream);

System.Drawing.Bitmap MyImage = new System.Drawing.Bitmap(imageStream);

httpResponse.Close();

imageStream.Close();

MyImage.Save(PATH+FileName, System.Drawing.Imaging.ImageFormat.Jpeg);