Loading image from code using relative path in Windows Forms
Asked Answered
S

2

6

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?

Sardella answered 28/6, 2011 at 19:7 Comment(4)
Why don't you embed the icons within your assembly?Hothouse
Check this link once #5609184Matriculation
I agree with Jon. You should embed images, especially icons. The only reason not to do this is if you wanted to dynamically modify the image.Kapoor
i am in the testing phases.. so i was not keen in putting images and removing them.. but i think in the end i will.. thanksSardella
A
23

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.

Assessment answered 28/6, 2011 at 19:12 Comment(0)
M
2

My issue was solved after this solution:

string[] s = { "\\bin" };
string path = Application.StartupPath.Split(s, StringSplitOptions.None)[0] + "\\Images\\On24.png";
Mainly answered 28/8, 2017 at 5:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.