Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading image from code using relative path in Windows Forms

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.

For example something like this:

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

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

like image 503
user175084 Avatar asked Jun 28 '11 19:06

user175084


2 Answers

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.

like image 180
agent-j Avatar answered Sep 28 '22 16:09

agent-j


My issue was solved after this solution:

string[] s = { "\\bin" };
string path = Application.StartupPath.Split(s, StringSplitOptions.None)[0] + "\\Images\\On24.png";
like image 27
Abdul Khaliq Avatar answered Sep 28 '22 15:09

Abdul Khaliq