在 Windows Phone 和 Silverlight 中将图像源转换为 BitmapImage
目前,我在将从 Feed 获得的图像字符串转换为 Windows Phone 7.1 中的 BitmapImage 时遇到问题。我正在使用 oData Northwind 示例 http://services.odata.org/Northwind/Northwind .svc/类别/。
这是类别中的实体之一。
<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");
}
});
}
I'm currently having problems in converting the image string that I got from Feed to BitmapImage in Windows Phone 7.1. I'm using oData Northwind sample http://services.odata.org/Northwind/Northwind.svc/Categories/.
This is the one of the entities from 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>
I'm using this code below to convert Picture string to BitmapImage but I'm getting "Unspecified error".. I tested in Silverlight 5 as well. I got no error but can't display the image. Is this encoding problem or something else?
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");
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
阅读此页。它会帮助你。
http://blogs.msdn.com/b/astoriateam/archive/2011/05/17/accessing-an-odata-media-resource-stream-from-a-windows-phone-7- application-streaming-provider-series-part-3.aspx
read this page. it will help you.
http://blogs.msdn.com/b/astoriateam/archive/2011/05/17/accessing-an-odata-media-resource-stream-from-a-windows-phone-7-application-streaming-provider-series-part-3.aspx