1
votes

I am developing a Metro App with c# and XAML.

In my app, I need to save the static map into a image like from the location(longitude, latitude): http://dev.virtualearth.net/REST/v1/Imagery/Map/Road/47.619048,-122.35384/15?mapSize=500,500&key=BINGKEY

I have tried many ways to create an image from it by sending the HTTPRequest to that URL, but all in vain.

For example:

HttpWebRequest httpWebRequest = (HttpWebRequest)result.AsyncState;
            WebResponse response = httpWebRequest.EndGetResponse(result);

            // Retrieve the reponse data.
            Stream responseStream = response.GetResponseStream();


            StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
            string xml = reader.ReadToEnd();

            System.Text.Encoding enc = System.Text.Encoding.UTF8;
            Clsses.Common.Instance.imageBytes = enc.GetBytes(xml);

            Stream stream = new MemoryStream(Clsses.Common.Instance.imageBytes);

            InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
            DataWriter writer = new DataWriter(randomAccessStream.GetOutputStreamAt(0));

            writer.WriteBytes(Clsses.Common.Instance.imageBytes);
            var result1 = await writer.StoreAsync();

            WriteableBitmap wb = new WriteableBitmapresult1 1);
            MemoryStream ms = new MemoryStream();
            using (IRandomAccessStream writeStream = await sampleFile1.OpenAsync(FileAccessMode.ReadWrite))
            {
                BitmapEncoder encoder = await BitmapEncoder.CreateAsync(new Guid(), writeStream);
                encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied, (uint)wb.PixelWidth, (uint)wb.PixelHeight, 96, 96, Clsses.Common.Instance.imageBytes);
                await encoder.FlushAsync();
            }

Please help me to Save it as an Image object.

1
Is the output from the above URL a picture? - Shoban
I haven't tried it but can you use the same technique that you can use in WP7 using WritableBitmap stackoverflow.com/questions/6299978/uielement-to-image-file-wp7 - Shoban
i.e load the map in a image control and then save it? - Shoban
@Shoban thanks,Ii tried this but it does not work in Metro app - Mandeep Kaur
Please post your code which does not work and may be it will be easier to fix that. - Shoban

1 Answers

0
votes
public async Task<StorageFile> GetBingMapImage(string ImageUrl)
{
    StorageFile storageFile = null;
    HttpClient httpClient = new HttpClient();
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, ImageUrl);
    HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
    if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK)
    {
        var fileSavePicker = new FileSavePicker();
        fileSavePicker.FileTypeChoices.Add("png", new List<string> { ".png" });
        fileSavePicker.SuggestedFileName = "map.png";
        fileSavePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        storageFile = await fileSavePicker.PickSaveFileAsync();

        if (storageFile != null)
        {
            IRandomAccessStream randomAccessStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite);
            DataWriter dataWriter = new DataWriter(randomAccessStream.GetOutputStreamAt(0uL));
            var bytes = await httpResponseMessage.Content.ReadAsByteArrayAsync();
            dataWriter.WriteBytes(bytes);
            await dataWriter.StoreAsync();
            await dataWriter.FlushAsync();
            await randomAccessStream.FlushAsync();
            dataWriter.Dispose();
            randomAccessStream.Dispose();
        }
    }
    return storageFile;
}