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>
ArgumentOutOfRangeException,ObjectDisposedExceptionor similar. You should also tell on what line the exception was thrown.