【问题标题】:Problem displaying images obtained in byte format from server显示从服务器以字节格式获取的图像时出现问题
【发布时间】:2011-09-28 09:06:25
【问题描述】:

我从服务器获取了一些图标文件 (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 chk 时,高度和宽度为 92X92);当我尝试显示它时,我最终显示的是空白而不是图像。我将这些图像连同我收到的名称绑定到一个列表框。名称显示没有任何问题。

【问题讨论】:

  • 见stackoverflow.com/questions/2097152/…,我认为它处理同样的问题
  • 它没有帮助,问题似乎很相似,但我不知道它对我没有帮助..
  • 图片是什么格式的? ICO? BMP?其他?
  • 它的 png 格式.. 代码运行良好 :) 现在可以在 UI 上看到图标 :)

标签: c# windows-phone-7 icons httpwebresponse bitmapimage


【解决方案1】:

我所做的更改是;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 2022-12-16
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    相关资源
    最近更新 更多