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

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

问题描述

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

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

public static string Correct_Icons = @"C:UsersxyzDocumentsVisual Studio 2008ProjectsFileShareMgmtFileShareMgmtResourcesCorrect.png";
public static string warning_Icon = @"C:UsersxyzDocumentsVisual Studio 2008ProjectsFileShareMgmtFileShareMgmtResourcesWarning.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  = "ResourcesCorrect.png";

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

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

推荐答案

对于我的程序,Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location) 返回C:code estJunkinDebug.

For my program, Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location) returns C:code estJunkinDebug.

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天全站免登陆