2012-04-19 80 views
1

我正在用c#和XAML開發Metro應用程序。使用c將地圖保存到地鐵應用程序中的圖像#

在我的應用程序,我需要在靜態地圖保存成圖像像是從位置(經度,緯度):http://dev.virtualearth.net/REST/v1/Imagery/Map/Road/47.619048,-122.35384/15?mapSize=500,500&key=BINGKEY

我已經發送的HTTPRequest到嘗試過很多方法可以從它創建一個圖像網址,但都是徒勞的。

例如:

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

以上URL的輸出是圖片嗎? – Shoban 2012-04-19 06:28:55

+0

是的輸出是一個JPEG圖像 – 2012-04-19 06:31:22

+0

我還沒有嘗試過,但你可以使用相同的技術,你可以在WP7使用WritableBitmap http://stackoverflow.com/questions/6299978/uielement-to-image-file-wp7 – Shoban 2012-04-19 06:33:08

回答

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; 
} 
相關問題