Windows phone 8 尝试加载图像时,Bitmap.SetSourceInternal中的索引超出范围
我犯了这个错误Windows phone 8 尝试加载图像时,Bitmap.SetSourceInternal中的索引超出范围,windows-phone-8,windows-phone,webclient,indexoutofboundsexception,Windows Phone 8,Windows Phone,Webclient,Indexoutofboundsexception,我犯了这个错误 System.IndexOutOfRangeException: Index was outside the bounds of the array. at System.Windows.Media.Imaging.BitmapSource.SetSourceInternal(Stream streamSource) 当我尝试加载这样的图像时: webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(we
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Windows.Media.Imaging.BitmapSource.SetSourceInternal(Stream streamSource)
当我尝试加载这样的图像时:
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(new Uri("http://zomerparkfeest.nl/uploads/" + currentItem.photo), webClient);
在opReadAsync中:
private void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled)
{
BitmapImage bitMapImage = new BitmapImage();
bitMapImage.SetSource(e.Result);
image.Source = bitMapImage;
loadingImagePBar.Visibility = Visibility.Collapsed;
imageLocked = false;
}
}
我并不总是会遇到这种异常,主要是在加载了很多图像之后。有什么问题吗?谢谢!我认为这解决了问题。我尝试了一段时间后出现了空指针,但似乎无法重新创建它。再次感谢!谢谢我认为这解决了问题。我尝试了一段时间后出现了空指针,但似乎无法重新创建它。再次感谢!谢谢我认为这解决了问题。我尝试了一段时间后出现了空指针,但似乎无法重新创建它。再次感谢!谢谢我认为这解决了问题。我尝试了一段时间后出现了空指针,但似乎无法重新创建它。再次感谢!
Use this by Memory stream this would work fine
private void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled)
{
BitmapImage bitMapImage = new BitmapImage();
Stream content=e.Result;
MemoryStream memoryStream = new MemoryStream();
content.CopyTo(memoryStream);
memoryStream.Position = 0;
byte[] buffer = null;
if (memoryStream != null && memoryStream.Length > 0)
{
BinaryReader binaryReader = new BinaryReader(memoryStream);
buffer = binaryReader.ReadBytes((int)memoryStream.Length);
}
Stream stream = new MemoryStream();
stream.Write(buffer, 0, buffer.Length);
stream.Seek(0, SeekOrigin.Begin);
bitMapImage.SetSource(stream);
image.Source = bitMapImage;
loadingImagePBar.Visibility = Visibility.Collapsed;
imageLocked = false;
}
}