我有一個帶有指定URL的Web圖像,它可以在瀏覽器中瀏覽。從WP8中的網址加載圖片
我試圖從網頁URL取得它,當程序進入bitmapImage.SetSource(ms);
我得到一個異常「
ex = {System.Exception: The component cannot be found. (Exception from HRESULT: 0x88982F50)
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.BitmapSource_SetSource(BitmapSource bitmapSource, CValue& byteStream)
at System.Wi...
」 我沒有搜查計算器其他問題...但沒有這方面的幫助。任何人都可以幫助我?
字節數組確實有數據,在運行時調試時,它返回imageByteArray = {byte [1227]};我的選擇是在將字節數組轉換爲BitmapImage時發生異常。
在HttpClient的包裝類:
public static async Task<Byte[]> GetWebImageByImageName(string ImageName)
{
//Uri imageServerUril = new Uri(ImageName);
var requestMessage = new HttpRequestMessage(HttpMethod.Get, ImageName);
var responseMessage = await client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead);
var responseData = await responseMessage.Content.ReadAsByteArrayAsync();
return responseData;
}
在視圖模型
:
private async void ReadArticleList(int pageNumber)
{
string webURL = "http://....."; // the web URL is no problem
try
{
byte[] imageByteArray = await CollectionHttpClient.GetWebImageByImageName(webURL);//
//Convert it to BitmapImage
using (MemoryStream ms = new MemoryStream(imageByteArray))
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.CreateOptions = BitmapCreateOptions.DelayCreation;
bitmapImage.SetSource(ms); // the exception got here
item.BitImage = bitmapImage;
}
IsLoading = false;
}
catch(Exception ex)
{
if (ex.HResult == -2146233088 && ex.Message.Equals("Response status code does not indicate success: 404()."))
{
MessageBox.Show("The network is not set right. Internet cannot be accessed.");
}
else
{
MessageBox.Show("sorry, no data.");
}
IsLoading = false;
}
}
*爲詳細*
- BitImage是的BitmapImage的實例;
- item.BitImage:產品文章
- 圖像格式的實例JPEG
文章模型是在下面:
public class Article : INotifyPropertyChanged
{
private long _Id;
public long ID
{
get { return _Id; }
set
{
if (_Id != value)
{
_Id = value;
NotifyPropertyChanged("ID");
}
}
}
private string _subject;
public string Subject
{
get
{
return _subject;
}
set
{
if (_subject != value)
{
_subject = value;
NotifyPropertyChanged("Subject");
}
}
}
private string _words;
public string Words
{
get
{
return _words;
}
set
{
if (_words != value)
{
_words = value;
NotifyPropertyChanged("Words");
}
}
}
private DateTime _publishDate;
public DateTime PublishDate
{
get
{ return _publishDate; }
set
{
if (_publishDate != value)
{
_publishDate = value;
NotifyPropertyChanged("PublishDate");
}
}
}
public List<string> ImagePathList = new List<string>();
public BitmapImage BitImage = new BitmapImage();
private string _firstImage;
public string FirstImage
{
get
{
return _firstImage;
}
set
{
if (_firstImage != value)
{
_firstImage = value;
NotifyPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
第一個問題,你爲什麼只是不要設置Image.Source =「http://...image.jpg」?你有沒有嘗試下載圖像作爲流而不是字節[]? –
我測試了你的代碼,它與我使用的testimages(包括jpegs)一起工作。您可以在設置URI時顯示圖像嗎?你仍然可以做的一件事是使用HttpClient的GetByteArrayAsync方法,因爲這將消除轉換回復的需要。 – Mark
@Martin Suchan,我不確定,Image.Source是異步或同步,所以我決定將自己的Web圖像異步處理;我試圖下載圖像作爲字節[],然後我需要將其轉換爲BitmapImage,流只是在字節[]和BitmapImage之間的溫度。 – max