从WP8中的Web URL加载图像

时间:2014-02-18 06:21:34

标签: c# windows-phone-7 windows-phone-8 bitmapimage dotnet-httpclient

我有一个带有指定网址的网络图片,可以在浏览器中浏览。

我试图从网址中检索它,当程序转到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...

” 我在stackoverflow上搜索了其他问题...但没有帮助。任何人都可以帮助我吗?

字节数组确实有数据,在运行时调试中,它返回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;
            }

        }

* for Detail *

  1. BitImage是BitmapImage的一个实例;
  2. item.BitImage:item是Article
  3. 的实例
  4. 图片格式为JPEG
  5. 文章模型如下:

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

3 个答案:

答案 0 :(得分:2)

如果您只想在不保存的情况下显示远程服务器中的图像,请执行以下操作:

imageControl1.Source = new BitmapImage(new Uri("http://delisle.saskatooncatholic.ca/sites/delisle.saskatooncatholic.ca/files/sample-1.jpg", UriKind.Absolute));

如果要将图像保存到IsolatedStorage,可以执行以下操作:

WebClient webClientImg = new WebClient();
webClientImg.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
webClientImg.OpenReadAsync(new Uri("http://delisle.saskatooncatholic.ca/sites/delisle.saskatooncatholic.ca/files/sample-1.jpg", UriKind.Absolute));

    void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        isSpaceAvailable = IsSpaceIsAvailable(e.Result.Length);
        if (isSpaceAvailable)
        {
            SaveToJpeg(e.Result);
        }
        else
        {
            MessageBox.Show("You are running low on storage space on your phone. Hence the image will be loaded from the internet and not saved on the phone.", "Warning", MessageBoxButton.OK);
        }
    }

检查IsolatedStorage空间是否可用的功能,否则它将不会下载图像。

    private bool IsSpaceIsAvailable(long spaceReq)
    {
        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            long spaceAvail = store.AvailableFreeSpace;
            if (spaceReq > spaceAvail)
            {
                return false;
            }
            return true;
        }
    }

如果空间可用,请使用以下函数将图像另存为IsolatedStorage:

    private void SaveToJpeg(Stream stream)
    {
        using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream isostream = iso.CreateFile("image1.jpg"))
            {
                BitmapImage bitmap = new BitmapImage();
                bitmap.SetSource(stream);
                WriteableBitmap wb = new WriteableBitmap(bitmap);
                // Encode WriteableBitmap object to a JPEG stream. 
                Extensions.SaveJpeg(wb, isostream, wb.PixelWidth, wb.PixelHeight, 0, 85);
                isostream.Close();

                LoadImageFromIsolatedStorage(); //Load recently saved image into the image control
            }
        }
    }

从IsolatedStorage加载图像控件:

    private void LoadImageFromIsolatedStorage()
    {
        byte[] data;

        try
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream isfs = isf.OpenFile("image1.jpg", FileMode.Open, FileAccess.Read))
                {
                    data = new byte[isfs.Length];
                    isfs.Read(data, 0, data.Length);
                    isfs.Close();
                }
            }
            MemoryStream ms = new MemoryStream(data);
            BitmapImage bi = new BitmapImage();
            bi.SetSource(ms);
            imageControl1.Source = bi;
        }
        catch
        {
        }
    }
  

从Google搜索中随机拍摄的图片。对图像的信任归于所有者。

希望这会有所帮助。 :)

答案 1 :(得分:1)

@Mark,你是对的,这可以根据你的建议运作。

我认为问题是如果我使用下面的代码,我得到的字节数组是字节[1227]

var requestMessage = new HttpRequestMessage(HttpMethod.Get, ImageName);
        var responseMessage = await client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead);

        var responseData = await responseMessage.Content.ReadAsByteArrayAsync();
        return responseData;

如果我使用var byteArray = await client.GetByteArrayAsync(ImageName);,则字节数组大小为byte [5996]

我不知道为什么会这样,但Mark的解决方案有效。

我的所有代码如下:

在MVVM模式中

        // get image from URL, ImageName is an absolute Url
        public static async Task<BitmapImage> GetWebImageByImageName(string ImageName)
        {
            //Uri imageServerUril = new Uri(ImageName);

            var byteArray = await client.GetByteArrayAsync(ImageName);


            //Convert byte array to BitmapImage       
            BitmapImage bitmapImage; 
            using (MemoryStream ms = new MemoryStream(byteArray))
            {
               bitmapImage = new BitmapImage();
                bitmapImage.SetSource(ms);
            }

            return bitmapImage;


        }
ViewModel中的

public void LoadPage(int pageNumber)
        {
            if (pageNumber == 1)
            {
                this.ArticleCollection.Clear();
            }

            IsLoading = true;
            ReadArticleList(pageNumber);

        }

private async void ReadArticleList(int pageNumber)
        {
            try
            {

                List<Article> articleList = new List<Article>();
                articleList = await CollectionHttpClient.GetArticlesByPageAsync(pageNumber);

                foreach (var item in articleList)
                {

                    item.BitImage =  await CollectionHttpClient.GetWebImageByImageName(item.ImagePathList[0]);


                    this.ArticleCollection.Add(item);

                }

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

        }

模型是:

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

        private ObservableCollection<string> _imagePathList = new ObservableCollection<string>();
        public ObservableCollection<string> ImagePathList
        {
            get { return this._imagePathList; }
            set
            {
                if (this._imagePathList != value)
                {
                    this._imagePathList = value;
                    // I'm going to assume you have the NotifyPropertyChanged
                    // method defined on the view-model
                    this.NotifyPropertyChanged();
                }
            }
        }

        BitmapImage _image;
        public BitmapImage BitImage
        {
            get
            {
                return _image;
            }
            set
            {
                if (ImagePathList.Any())
                {
                    value = new BitmapImage(new Uri(ImagePathList.FirstOrDefault(), UriKind.RelativeOrAbsolute));
                    _image = value;
                }
            }
        }

        private Uri _firstImage;
        public Uri FirstImage
        {
            get
            {
                return _firstImage;
            }
            set
            {
                if (_firstImage != value)
                {
                    _firstImage = value;
                    NotifyPropertyChanged("FirstImage");
                }
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

答案 2 :(得分:0)

首先,如果您有网址,您可以将网址提供给绑定参数,它会自动显示图像,或者您是否要下载图像并保存图像。然后显示它,所以这里是字节到图像的代码&amp;图像到字节。

    {
        BitmapImage image = new BitmapImage();
        MemoryStream ms = new MemoryStream();
        WriteableBitmap wb = new WriteableBitmap(image);
        wb.SaveJpeg(ms, image.PixelWidth, image.PixelHeight, 0, 100);
        imageBytes = ms.ToArray();
    }

使用它来保存图像,因为我正在使用我的代码。