如何在 C# 项目中设置图像目录的相对路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15696812/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:37:58  来源:igfitidea点击:

How to set relative path to Images directory inside C# project?

c#relative-path

提问by nzdev

I am working on C# project i need to get the images from Images directory using relative path. I have tried

我正在处理 C# 项目,我需要使用相对路径从 Images 目录中获取图像。我试过了

var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"\Images\logo.png";
var logoImage = new LinkedResource(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)+@"\Images\logo.png")

But no luck with these... I have made the images to be copied to output directory when the program is running but it doesn't pickup those images.

但是这些没有运气......我已经在程序运行时将图像复制到输出目录,但它没有拾取这些图像。

采纳答案by Hasanka Rathnayake

If you are using LinkedResource() in C# it is most likely not to pickup your relative URI or the file location.

如果您在 C# 中使用 LinkedResource(),则很可能不会获取您的相对 URI 或文件位置。

You can use some extra piece of code

您可以使用一些额外的代码

var outPutDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
var logoimage = Path.Combine(outPutDirectory, "Images\logo.png");
string relLogo = new Uri(logoimage).LocalPath;
var logoImage = new LinkedResource(relLogo)

Now it will pickup your relative path, convert this to absolute path in memory and it will help you get the images.

现在它将获取您的相对路径,将其转换为内存中的绝对路径,它将帮助您获取图像。

回答by J.C

First, add those image file to your project (create an Image folder is a good idea)

首先,将这些图像文件添加到您的项目中(创建一个图像文件夹是个好主意)

Second, select the image in your solution manager, and view the property window.

其次,在解决方案管理器中选择图像,然后查看属性窗口。

And then, change the "copy to output folder" to "always" or "copy when update".

然后,将“复制到输出文件夹”更改为“始终”或“更新时复制”。

PS. My IDE is Trad. Chinese so I can not ensure the correct keywords in your language.

附注。我的 IDE 是 Trad。中文所以我无法确保您的语言中的关键字正确。

回答by rgunawan

I would make sure that the Images directory is in the output folder. I usually use Assembly.GetExecutingAssembly().Locationto get the location of my dll.

我会确保图像目录在输出文件夹中。我通常Assembly.GetExecutingAssembly().Location用来获取我的 dll 的位置。

However, for images, I usually use the Resources page/collection in the project's Properties page. Hereis more information about it. Putting the image in the project's Resource would automatically give you an easy way to access it.

但是,对于图像,我通常使用项目属性页面中的资源页面/集合。是有关它的更多信息。将图像放在项目的资源中会自动为您提供一种访问它的简单方法。

For more information about GetExecutingAssembly: MSDN Page

有关 GetExecutingAssembly 的更多信息:MSDN 页面

回答by Hasanka Rathnayake

if u want to display images in your folder using your application use an array and put all pictures in ur folder into array. then you can go forward and backward.

如果您想使用您的应用程序在您的文件夹中显示图像,请使用数组并将您文件夹中的所有图片放入数组中。然后你可以前进和后退。

string[] _PicList = null;

int current = 0;

_PicList = System.IO.Directory.GetFiles("C:\Documents and Settings\Hasanka\
                                               Desktop\deaktop21052012\UPEKA","*.jpg"); 
// "*.jpg" will select all 

//pictures in your folder


String str= _PicList[current];

DisplayPicture(str);

private void DisplayPicture(string str)

{
    //throw new NotImplementedException();
    BitmapImage bi = new BitmapImage(new Uri(str));
    imagePicutre.Source = bi; // im using Image in WPF 
    //if u r using windows form application it must be a PictureBox i think.

    label1.Content = str;

}