使用Windows窗体中的相对路径从代码加载图像 [英] Loading image from code using relative path in Windows Forms

查看:59
本文介绍了使用Windows窗体中的相对路径从代码加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个 Image ,在WinForms中有一张图片.

I have an Image in my application and I have a picture in my WinForms.

public static string Correct_Icons = @"C:\Users\xyz\Documents\Visual Studio 2008\Projects\FileShareMgmt\FileShareMgmt\Resources\Correct.png";
public static string warning_Icon = @"C:\Users\xyz\Documents\Visual Studio 2008\Projects\FileShareMgmt\FileShareMgmt\Resources\Warning.png";


cell.Value = Image.FromFile("Resources/warning_Icon);

但是我只想使用相对路径,而不是上面的完整路径.

But I just want to use a relative path and not the full path like above.

例如这样的东西:

public static string Correct_Icons  = "\Resources\Correct.png";

并继续...../不工作.有什么建议吗?

and cont. ..../ not working. Any suggestions?

推荐答案

对于我的程序, Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)返回 C:\ code \ test \ Junk \ bin \ Debug .

cell.Value = Image.FromFile(
  Path.Combine (
     Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location),
     "Resources/warning_Icon"));

当然,通常除非在不重新编译的情况下更改它们,否则通常将这些资源嵌入到程序集中.

Of course, usually you would embed the resources in your assembly unless you want to change them without a recompile.

这篇关于使用Windows窗体中的相对路径从代码加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆