【问题标题】:Index out of bounds in Bitmap.SetSourceInternal, when trying to load an image尝试加载图像时,Bitmap.SetSourceInternal 中的索引超出范围
【发布时间】:2013-12-20 13:33:04
【问题描述】:

我收到了这个错误

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

我并不总是得到这个异常,它主要是在加载了很多图像之后。有什么问题?

【问题讨论】:

    标签: windows-phone-8 windows-phone webclient indexoutofboundsexception


    【解决方案1】:
    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;
                }
         }
    

    【讨论】:

    • 谢谢!我认为这解决了这个问题。在我尝试后一段时间出现了一个空指针,但我似乎无法重新创建它。再次感谢!
    猜你喜欢
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 2017-09-29
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多