2011-12-02 39 views
0

我目前在將從Feed獲取的圖像字符串轉換爲Windows Phone 7.1中的BitmapImage時出現問題。我正在使用oData Northwind示例http://services.odata.org/Northwind/Northwind.svc/Categories/在Windows Phone和Silverlight中將圖像Feed轉換爲BitmapImage

這是來自類別的實體之一。

<entry> 
<id>http://localhost:32026/Northwind/Northwind.svc/Categories(1)</id> 
<title type="text" /> 
<updated>2011-11-20T20:36:50Z</updated> 
- <author> 
<name /> 
</author> 
<link rel="edit" title="Category" href="Categories(1)" /> 
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Products" type="application/atom+xml;type=feed" title="Products" href="Categories(1)/Products" /> 
<category term="NorthwindModel.Category" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
- <content type="application/xml"> 
- <m:properties> 
<d:CategoryID m:type="Edm.Int32">1</d:CategoryID> 
<d:CategoryName>Beverages</d:CategoryName> 
<d:Description>Soft drinks, coffees, teas, beers, and ales</d:Description> 
<d:Picture m:type="Edm.Binary">...{data here}.....</d:Picture> 
</m:properties> 
</content> 
</entry> 

我用下面這段代碼轉換圖片串的BitmapImage但我得到「未指定的錯誤」 ..我在Silverlight 5測試也是如此。我沒有錯誤,但無法顯示圖像。這是編碼問題還是別的?

public void LoadData() { 

      HttpWebRequest request = 
       (HttpWebRequest)HttpWebRequest.Create(@"http://services.odata.org/Northwind/Northwind.svc/Categories/"); 
      request.Method = "GET"; 
      request.BeginGetResponse(new AsyncCallback(ReadCallback), request); 

     } 

     private void ReadCallback(IAsyncResult result) { 

      Deployment.Current.Dispatcher.BeginInvoke(() => { 
       HttpWebRequest request = (HttpWebRequest)result.AsyncState; 
       HttpWebResponse response = request.EndGetResponse(result) 
               as HttpWebResponse; 

       XNamespace nsBase = "http://services.odata.org/Northwind/Northwind.svc/Categories/"; 
       XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices"; 
       XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"; 
       XNamespace atom = "http://www.w3.org/2005/Atom"; 

       if (response.StatusCode == HttpStatusCode.OK) { 
        var xdoc = XDocument.Load(response.GetResponseStream()); 
        foreach (var entity in xdoc.Descendants(atom + "entry")) { 
         var properties = entity.Element(atom + "content") 
               .Element(m + "properties"); 

         byte[] byteArray = Convert.FromBase64String(properties.Element(d + "Picture").Value); 
         var bitmapImage = new BitmapImage() { CreateOptions = BitmapCreateOptions.DelayCreation }; 
         MemoryStream stream = new MemoryStream(byteArray); 
         //stream.Read(byteArray, 0, Convert.ToInt32(stream.Length)); 
         //stream.Seek(0, SeekOrigin.Begin); 
         bitmapImage.SetSource(stream); 

         var category = new CategoryModel() { 
          Id = Convert.ToInt32(properties.Element(d + "CategoryID").Value), 
          Name = properties.Element(d + "CategoryName").Value, 
          Description = properties.Element(d + "Description").Value, 
          Picture = bitmapImage 
         }; 
         Items.Add(category); 
        } 
       } 
       else { 
        MessageBox.Show("Exception"); 
       } 
      }); 

     } 

回答

0
+0

謝謝!該帖子和我的代碼之間的區別是,我沒有使用自動生成的代理或oData客戶端。正如您可以在http://services.odata.org/Northwind/Northwind.svc/Categories/。這個鏈接中看到的那樣,這些圖片是基於64位格式的。我試圖將該圖像字符串轉換爲BitmapImage .. –

+0

很久以前,我讀到Northwind數據庫首先用MS Access創建,存儲的圖像有78個字節的OLE頭添加。我想你需要從圖像數據中去掉78字節的頭文件。 –

相關問題