1

我正在使用 c# 和 XAML 开发 Metro App。

在我的应用程序中,我需要将静态地图保存为来自位置(经度、纬度)的图像:http: //dev.virtualearth.net/REST/v1/Imagery/Map/Road/47.619048,-122.35384/15 ?mapSize=500,500&key=BINGKEY

我尝试了许多方法来通过将 HTTPRequest 发送到该 URL 来从中创建图像,但都是徒劳的。

例如:

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();
            }

请帮我将其另存为图像对象。

4

1 回答 1

0
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;
}
于 2013-03-26T10:10:13.410 回答