2011-09-28 49 views
0

我从服务器获取几个图标文件(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); 
} 

现在我的问题是,我不能够获得开放的,我不甚至得到图标(但是当我把断点ñ chk的高度和宽度都是92X92);当我尝试显示它时,我最终显示空白而不是图像。我将这些图像与我收到的名称一起绑定到一个列表框。名称显示没有任何问题。

+0

见http://stackoverflow.com/questions/2097152/creating-wpf-bitmapimage-from-memorystream-png-gif,我认为它具有相同的问题 – tazyDevel

+0

这没有帮助的交易,这个问题似乎相似但我不知道它是不是帮助我.. – Apoorva

+0

什么格式的图像来过? ICO? BMP?其他? – ctacke

回答

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); 
    }