我有网络图片,网址是正确的,因为我可以在网络浏览器中看到它。
我将web url读入byte []并将byte []转换为BitmapImage。
但是当调试器转到“bitmapImage.SetSource(ms);”时,它会转到例外:
StackTrace = " at MS.Internal.XcpImports.CheckHResult(UInt32 hr)\r\n
at MS.Internal.XcpImports.BitmapSource_SetSource(BitmapSource bitmapSource, CValue& byteStream)\r\n
at System.Windows.Media.Imaging.BitmapSource.SetSourceInternal(Stream streamSource)\r\n at Sy...
在我的CollectionHttpClient.cs
中// get image from URL
public static async Task<Byte[]> GetImageByImageName(string ImageName)
{
//Uri imageServerUril = new Uri(ImageName);
var requestMessage = new HttpRequestMessage(HttpMethod.Get, ImageName);
var responseMessage = await client.SendAsync(requestMessage);
var responseData = await responseMessage.Content.ReadAsByteArrayAsync();
return responseData;
}
在我的视图模型中
try
{
...........
byte[] imageByteArray = await CollectionHttpClient.GetImageByImageName(webImageUrl);
using (MemoryStream ms = new MemoryStream(imageByteArray))
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.CreateOptions = BitmapCreateOptions.DelayCreation;
bitmapImage.SetSource(ms); // exception occurs here
item.BitImage = bitmapImage;
}
............
}
catch(exception ex)
{
....
}