Problem displaying images obtained in byte format from server
I am getting a few icon files (92X92) from a server. I need to parse them and store them in a dictionary and later display them on the UI. I am using the following code for obtaining the File Name and for other action:
System.Windows.Media.Imaging.BitmapImage icon = null;
using (AutoResetEvent are = new AutoResetEvent(false))
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MemoryStream byteStream = new MemoryStream(resp);
byteStream.Write(resp, 0, resp.Length);
icon = new BitmapImage();
icon.SetSource(byteStream);
Console.WriteLine(icon.PixelHeight + ":" + icon.PixelWidth);
string[] iconname = entry.Name.Split(new char[] { '-' });
string newimagename = iconname[1];
are.Set();
string[] newname = entry.Name.Split(new char[] { '-', '.' });
iconDict.Add(newimagename, icon);
});
are.WaitOne();
//string[] newname = entry.Name.Split(new char[] { '-', '.' });
//string newFileName = newname[1];
//iconDict.Add(newFileName, icon);
}
Now my problem is that I am not able to obtain the Uri and am not even getting the icon(but when i put breakpoint n chk the height and width are 92X92); when I try to display it I end up displaying blank space instead of开发者_运维问答 images. I am binding these images to a listbox along with their name that I receive. Names are getting displayed without any problem.
changes i made are ;
using (AutoResetEvent are = new AutoResetEvent(false))
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MemoryStream byteStream = new MemoryStream(resp);
byteStream.Write(resp, 0, resp.Length);
string[] iconname = entry.Name.Split(new char[] { '-' });
string newimagename = iconname[1];
Uri icon_url = new Uri(newimagename, UriKind.RelativeOrAbsolute);
icon = new BitmapImage(icon_url);
imag = new Image();
imag.Source = icon;
sourceofImage = icon.UriSource.ToString();
icon.SetSource(byteStream);
Console.WriteLine(icon.PixelHeight + ":" + icon.PixelWidth);
are.Set();
// string[] newname = entry.Name.Split(new char[] { '-', '.' });
// iconDict.Add(sourceofImage, icon);
});
are.WaitOne();
string[] newname = entry.Name.Split(new char[] { '-', '.' });
string newFileName = newname[1];
iconDict.Add(newFileName, icon);
}
精彩评论