1

c# と XAML を使用して Metro アプリを開発しています。

私のアプリでは、静的マップを場所 (経度、緯度) のような画像に保存する必要があります: 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();
            }

Imageオブジェクトとして保存するのを手伝ってください。

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 に答える