在Windows Phone和Silverlight中将图像Feed转换为BitmapImage

时间:2011-12-02 08:06:33

标签: silverlight rest odata windows-phone-7 bitmapimage

我目前在将Windows Feed 7.1中从Feed转换为BitmapImage的图像字符串时遇到问题。我正在使用oData Northwind示例http://services.odata.org/Northwind/Northwind.svc/Categories/

这是类别中的一个实体。

<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>

我正在使用下面的代码将Picture字符串转换为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");
                }
            });

        }