简体   繁体   English

将URI设置为图像源应用程序中的根目录

[英]Setting URI to root directory in application for image source

How do you set the URI for an image source to the root directory (ie same folder as your exe) in a WPF application? 如何在WPF应用程序中将图像源的URI设置到根目录(即与exe相同的文件夹)?

var myImage = new Image();
myImage.Source = new BitmapImage(new Uri("pack://application:,,,/AssemblyName;component/speedline.png", UriKind.Absolute));

I've tried the above and keep getting the following error message: 我已经尝试了上述方法,并不断收到以下错误消息:

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll. mscorlib.dll中发生了'System.IO.FileNotFoundException'类型的未处理异常。 Additional information: Could not load file or assembly 'AssemblyName, Culture=neutral' or one of its dependencies. 附加信息:无法加载文件或程序集'AssemblyName,Culture = neutral'或其依赖项之一。 The system cannot find the file specified. 该系统找不到指定的文件。

I am obviously doing something very wrong, but can guarantee that the png file can be found at C:\\Users\\Admin\\Documents\\Visual Studio 2013\\Projects\\Speedy\\Speedy\\bin\\Debug , in the same folder as my exe file, but how do I reference it? 我显然做错了什么,但可以保证png文件可以在exe文件所在的文件夹中的C:\\Users\\Admin\\Documents\\Visual Studio 2013\\Projects\\Speedy\\Speedy\\bin\\Debug中找到,但是我该如何引用呢?

Create a new folder under the solution explorer called component and add the existing image under that. 在称为组件的解决方案资源管理器下创建一个新文件夹,并在该文件夹下添加现有图像。 Now try using the Relative path. 现在尝试使用相对路径。

myImage = new BitmapImage(new Uri("/component/speedline.png", UriKind.Relative));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM