0

Hi i have been looking for a solution to display images in image control that is inside the list box.I have seen to set image source and assigning it new BitmapImage(new Uri(stringX)).

Not in my case i first retrieve all the images from URL using WebClient that is in function and that function returns MemoryStream after some process.

Now what i want to do is display that image so i don't have Uri to create new bitmap.So i tried to Implement StreamSource but i get

  Set property 'System.Windows.Media.Imaging.BitmapImage.StreamSource' threw an exception.

here goes my code

Retrieve Image From Web

public MemoryStream GetImage(string id)
        {
            WebResponse result = null;
            Image rImage = null;
            MemoryStream imageStream = null;
            try
            {
                string url = "https://devnmark.com/" + id + "/picture";
                WebRequest request = WebRequest.Create(url);
                result = request.GetResponse();
                Stream stream = result.GetResponseStream();
                BinaryReader br = new BinaryReader(stream);
                byte[] rBytes = br.ReadBytes(1000000);
                br.Close();
                result.Close();
                imageStream = new MemoryStream(rBytes, 0, rBytes.Length);
                imageStream.Write(rBytes, 0, rBytes.Length);
                rImage = Image.FromStream(imageStream, true);
               // imageStream.Close();
            }
            catch (Exception c)
            {
                //MessageBox.Show(c.Message);
            }
            finally
            {
                if (result != null) result.Close();
            }
            return imageStream;

        }

Class Declared for Type

 class UserInfo
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public bool IsChecked { get; set; }
        public MemoryStream Picture { get; set; }
    }

Load Images

 private void LoadFriends()
        {
            foreach (dynamic imge in MainList)
            {
                if (x >= 6)
                    break;
                UserInfo info = new UserInfo();
                info.Id = int.Parse(imge.id);
                info.Name = imge.name;         
                info.Picture = function.GetImage(info.Id.ToString());
                FriendList.Add(info);
                x++;
            }
            list.ItemsSource = FriendList;
        }

XMAL for ListBox

<ListBox x:Name="list"  Margin="18,100,535,74" >
    <ListBox.ItemTemplate>
       <HierarchicalDataTemplate>
          <StackPanel Orientation="Horizontal">
             <Image Height="50" Width="50">
                 <Image.Source>                                    
                   <BitmapImage StreamSource="Picture" ></BitmapImage>
                 </Image.Source>
             </Image>
             <my:RibbonCheckBox Label="{Binding Name}" IsChecked="{Binding IsChecked}" />                    
          </StackPanel>                     
      </HierarchicalDataTemplate>      
    </ListBox.ItemTemplate>                 
</ListBox>
3
  • Do you mind telling what exception that was thrown? Commented Mar 12, 2011 at 13:50
  • I have mentioned it in question Commented Mar 12, 2011 at 14:12
  • no the Exception should be like ArgumentOutOfRangeException, ObjectDisposedException or similar. You should also tell on what line the exception was thrown. Commented Mar 12, 2011 at 14:26

2 Answers 2

2

You have a big bunch of problems.

byte[] rBytes = br.ReadBytes(1000000);

What if the image is larger than 1MB?

Remove the line

rImage = Image.FromStream(imageStream, true);

You don't use the result from that, it just will take processing time and position the imageStream at the end.

You should probably use MemoryStream.Seek() to reset the stream to it starting position before returning.

imageStream.Seek(0, SeekOrigin.Begin);

Edit

Your XAML binding is wrong

<BitmapImage StreamSource="Picture" ></BitmapImage>

Should be

<BitmapImage StreamSource="{Binding Picture}" ></BitmapImage>

to be a valid binding, but I'm actually not sure if you can bind StreamSource at all or if you need to initialize from code, like in the example at the very bottom of the msdn doc.

Sign up to request clarification or add additional context in comments.

6 Comments

Still getting Exception mentioned above
@Afnan, You have still not told anyone what exception was thrown.
Exception Title = XmlParseException and it reads Set property 'System.Windows.Media.Imaging.BitmapImage.StreamSource' threw an exception.
@Afnan That is probably because your binding is incorrectly declared, I updated my answer.
Yes this was the problem.Thanks you helped me alot.
|
0

The website seems to require some sort of authentication as far as I can tell. Did you provide that? Could you give us a working url?

1 Comment

the problem is not in the website if gives the result and if i add a break point i see that there is an BitmapImage there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.