Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从资源项目中获取图像_C#_Wpf_Image - Fatal编程技术网

C# 如何从资源项目中获取图像

C# 如何从资源项目中获取图像,c#,wpf,image,C#,Wpf,Image,我的项目中有这个包: 项目名称:Gestore ---视图(文件夹) ------资源(文件夹) ---------图像(文件夹) ------------immagine1.png ---DatiAnagraficiPageView.xaml 现在我想以编程方式创建一个图像,所以我尝试编写此代码,但没有找到它。代码是: System.Windows.Controls.Image img = new System.Windows.Controls.Image(); img.Source = ne

我的项目中有这个包:

项目名称: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()”

中,假设immagine1.png是您应该使用的资源,例如

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

假设immagine1.png是您应该使用的资源,例如

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

GetCurrentDirectory()不提供调用的路径,而是提供执行可执行文件的路径


目录。GetCurrentDirectory()不提供调用的路径,而是提供执行可执行文件的路径

试试看

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;
试一试


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