【问题标题】:Dynamically adding and loading image from Resources in C#在 C# 中从资源中动态添加和加载图像
【发布时间】:2020-02-11 23:14:44
【问题描述】:

我的解决方案中添加了一些图像,现在它位于解决方案资源管理器内的文件夹 images\flowers\rose.png 下。我想要一种将此图像动态加载到我的图像控件的方法。

我目前的方法是使类型为“内容”并使用“始终复制”属性。然后我会给出图像的相对路径,如下所示。

Image2.Source = new BitmapImage(new Uri("/images/flowers/Customswipe_b.png", UriKind.Relative));

有没有办法让它从资源中加载而不复制到目标系统。

【问题讨论】:

    标签: c# wpf image resources


    【解决方案1】:

    以下对我来说很好用:

    image.Source = new BitmapImage(new Uri("pack://application:,,,/YourAssemblyName;component/Resources/someimage.png", UriKind.Absolute));
    

    您还应该将图像的Build ActionNone 更改为Resource

    【讨论】:

      【解决方案2】:

      您可以打开资源编辑器(解决方案资源管理器,单击 Resources.resx)并在其中添加图像。然后你可以简单地以BitmapProperties.Resources.ImageId 访问它

      http://msdn.microsoft.com/en-us/library/3bka19x4(v=vs.100).aspx

      【讨论】:

        【解决方案3】:

        我在查找 URI 的确切语法时遇到了一些问题,因此请参阅下面的更多详细信息:

        如果您的图片 (myImage.png) 位于子文件夹“images”中(来自根目录),则确切的语法是:

        image.Source = new BitmapImage(new Uri(@"pack://application:,,,/images/myImage.png", UriKind.Absolute));
        

        如果您的图像位于子文件夹images/icon/(来自根目录)中,则语法为:

        image.Source = new BitmapImage(new Uri(@"pack://application:,,,/images/icon/myImage.png", UriKind.Absolute));
        
        • 请注意,"pack://application:,,, 部分不会更改。
        • 务必将“构建操作”设置为“资源”

        欲了解更多信息:see here

        【讨论】:

          猜你喜欢
          • 2012-11-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-14
          • 1970-01-01
          相关资源
          最近更新 更多