我正在使用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();
}
请帮我将其另存为图片对象。
答案 0 :(得分: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;
}