Jermartyno
Jermartyno

Reputation: 43

Getting image path from Project folder in WinForms

I want to access my folder that contains Image files.
The folder is located inside my project. Here is the look on my Explorer:

[1]

I am not able to access it through code when I do the following: Vanilla_Icons_Installer.Images.

How can I access Image1.png inside Images folder by doing PreviewPicture.Image = path?

Upvotes: 3

Views: 6876

Answers (2)

Stan1k
Stan1k

Reputation: 360

In WPF you can use a relative path inside your XAML like this:

<Image x:Key="Image" Source="../Images/Image1.png" />

But in your screenshot I see that you are using WinForms and you want to set it through code.
Therefore you could try this:

PreviewPicture.Image = Image.FromFile(
  Path.Combine (
     Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location),
     "Images/Image1.png"));

Don't forget to set the Copy to Output directory option to e.x. "Copy if newer" for your Image file.
The above code will create a Images directory in your bin/debug folder with the image inside.

Upvotes: 2

Jermartyno
Jermartyno

Reputation: 43

Actually this fixed my problem:

PreviewPicture.Image = Image.FromFile(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images", "LightColorful", "ClassImages.png"));

And setting the Copy to Output property of Image to Copy if newer.

Upvotes: 1

Related Questions