【问题标题】:How to get Image from resource project如何从资源项目中获取图像
【发布时间】:2014-09-24 19:50:30
【问题描述】:

我的项目中有这个包:

项目名称:Gestore

---查看(文件夹)

------资源(文件夹)

---------图片(文件夹)

------------immagine1.png

---DatiAnagraficiPageView.xaml

现在我想以编程方式创建一个图像,所以我尝试编写此代码但没有找到。代码是:

System.Windows.Controls.Image img = new System.Windows.Controls.Image();
img.Source = new BitmapImage(new Uri(Directory.GetCurrentDirectory() + @"\Resources\Images\immagine1.png"));

我有一个异常,因为控件找不到图像。

我们能帮帮我吗?

我认为错误在代码“Directory.GetCurrentDirectory()”中

【问题讨论】:

  • Directory.GetCurrentDirectory() 返回什么?
  • 它返回“C:\\Users\\michele.castriotta\\Source\\Workspaces\\Omniacare\\software\\exercise platform\\Gestore\\Gestore\\bin\\Debug”
  • 返回bin 目录的路径......在这种情况下没那么有用。请参阅 MSDN 上的Pack URIs in WPF 页面。

标签: c# wpf image


【解决方案1】:

假设 immagine1.png 是您应该使用的资源 Pack URI,类似于

new Uri("pack://application:,,,/Gestore;component/View/Resources/Images/immagine1.png")

【讨论】:

    【解决方案2】:

    Directory.GetCurrentDirectory() 不会为您提供调用的路径,而是执行可执行文件的路径。

    http://msdn.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory(v=vs.110).aspx

    【讨论】:

      【解决方案3】:

      试试

      System.Windows.Controls.Image img = new System.Windows.Controls.Image();
      BitmapImage bm = new BitmapImage(new Uri("@View/Resources/Images/immagine1.png", UriKind.RelativeOrAbsolute));
                  img.Source = bm;
      

      【讨论】:

        猜你喜欢
        • 2022-08-23
        • 1970-01-01
        • 2020-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-24
        • 2023-03-11
        • 1970-01-01
        相关资源
        最近更新 更多