我从服务器获取了一些图标文件(92X92)。我需要解析它们并将它们存储在字典中,然后在UI上显示它们。我使用以下代码获取文件名和其他操作:
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);
}
现在我的问题是我无法获得Uri并且甚至没有获得图标(但是当我把断点n时,高度和宽度都是92X92);当我尝试显示它时,我最终显示空白而不是图像。我将这些图像绑定到列表框以及我收到的名称。名称显示没有任何问题。
答案 0 :(得分:0)
我做的改变是;
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);
}