Reputation: 7419
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>
Upvotes: 0
Views: 2324
Reputation: 47038
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.
Upvotes: 2
Reputation: 50672
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?
Upvotes: 0