WPF 从文件夹加载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22661912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF load image from folder
提问by Dim
I have images that is not located in resources but on the disk. The folder is relative to application. I used:
我的图像不在资源中,而是在磁盘上。该文件夹是相对于应用程序的。我用了:
Overview_Picture.Source = new BitmapImage(new Uri(String.Format("file:///{0}/../MyImages /myim.jpg", Directory.GetCurrentDirectory())));
Overview_Picture.Source = new BitmapImage(uriSource);
But those type of code created many problems and messed up GetCurrentDirectory returns that sometime ok and some times not.
但是这些类型的代码造成了许多问题,并且弄乱了 GetCurrentDirectory 返回的结果,有时可以,有时不可以。
So, MyImages folder is located next to Debug folder, how can I use them images there and not as I done, In some other more right way?
因此,MyImages 文件夹位于 Debug 文件夹旁边,我如何在那里使用它们而不是像我所做的那样,以其他更正确的方式使用它们?
回答by Thorsten Dittmar
As mentioned oftentimes here on SO, the GetCurrentDirectorymethod does by definition not always return the directory your assembly resides in, but the current working directory. There is a big difference between the two.
正如在 SO 上经常提到的GetCurrentDirectory那样,根据定义,该方法并不总是返回程序集所在的目录,而是返回当前工作目录。两者有很大的不同。
What you need is the current assembly folder (and the parent of that). Also, I'm not sure whether it is wanted that the pictures are one folder above the installation folder (which is basically what you're saying when you say they are one level above the Debugfolder - in real life that would be one folder above the folder the application is installed to).
您需要的是当前程序集文件夹(及其父文件夹)。另外,我不确定是否希望图片位于安装文件夹上方的一个文件夹中(这基本上就是您所说的,当您说它们位于Debug文件夹上方的一层时- 在现实生活中,这将是上方的一个文件夹应用程序安装到的文件夹)。
Use the following:
使用以下内容:
string currentAssemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string currentAssemblyParentPath = Path.GetDirectoryName(currentAssemblyPath);
Overview_Picture.Source = new BitmapImage(new Uri(String.Format("file:///{0}/MyImages/myim.jpg", currentAssemblyParentPath)));
Also, there's a stray space after MyImages, which I removed.
此外,在之后有一个杂散空间MyImages,我已将其删除。
回答by Clemens
An alternative to constructing an absolute Uri from a relative file path would be to just open a FileStream from the relative path, and assign that to the BitmapImage's StreamSourceproperty. Note however that you also have to set BitmapCacheOption.OnLoadwhen you want to close the stream right after initializing the BitmapImage.
从相对文件路径构造绝对 Uri 的另一种方法是从相对路径打开 FileStream,并将其分配给 BitmapImage 的StreamSource属性。但是请注意,您还必须设置BitmapCacheOption.OnLoad何时要在初始化 BitmapImage 后立即关闭流。
var bitmap = new BitmapImage();
using (var stream = new FileStream("../MyImages/myim.jpg", FileMode.Open))
{
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.EndInit();
bitmap.Freeze(); // optional
}
Overview_Picture.Source = bitmap;

