Loading image from relative path in Windows Forms

风格不统一 提交于 2019-11-26 09:57:40

问题


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 want just want the relative path and not the full path.

eg i want this

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

and cont. ..../ not working


回答1:


For my program, Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location) returns 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.




回答2:


My issue was solved after this solution:

string[] s = { "\\bin" };
string path = Application.StartupPath.Split(s, StringSplitOptions.None)[0] + "\\Images\\On24.png";


来源:https://stackoverflow.com/questions/6511816/loading-image-from-relative-path-in-windows-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!