【问题标题】:How to make Uri reference to a Resource image embedded in the assembly itself? [duplicate]如何使 Uri 引用嵌入在程序集本身中的资源图像? [复制]
【发布时间】:2014-03-26 14:45:41
【问题描述】:

在我的装配项目中,我有一个 .png 图像,它的 Build Action 设置为 Resource。

现在我想做以下事情:

_myIcon = new BitmapImage(new Uri(**path to the image embedded on the assembly**));

我查看了http://msdn.microsoft.com/en-us/library/aa970069(v=vs.110).aspx 中的示例,但据我所知,“URI 无效:无法确定 URI 的格式。”。我觉得这应该不是一件复杂的事情,我是不是走错了路?

【问题讨论】:

    标签: c# wpf image uri


    【解决方案1】:

    另一个答案是有效的,但不会产生 BitmapSource。

    正确的答案是使用Resource File Pack URI

    _myIcon = new BitmapImage(new Uri("pack://application:,,,/Images/MyImage.png");
    

    【讨论】:

      【解决方案2】:

      对我来说,它适用于这样的东西

      String filename = this.GetType().Namespace + ".Resources.myimg.bmp";
      //or String.Join if you like
      _myIcon = Bitmap.FromStream(Assembly.GetExecutingAssembly()
                                      .GetManifestResourceStream(filename))
      

      所以 FromStream + GetManifestResourceStream 应该是神奇的词

      【讨论】:

      • GetManifestResourceStream 出于某种原因返回 null。为要嵌入的图像放置 Build Action = Resource 就足够了吗?还是我需要更多的东西?我也尝试了filename 的一些变体,但也没有成功。感谢您的回答。
      • 构建操作必须Embedded Resource 而不是Resource
      • @RohitVats 这似乎很明显,我没有注意到我觉得很愚蠢。谢谢,现在它不再返回 null 了。
      • Bitmap 类在什么命名空间中?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      相关资源
      最近更新 更多