Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting image path from Project folder in WinForms

Tags:

c#

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?

like image 799
Jermartyno Avatar asked Dec 17 '25 20:12

Jermartyno


1 Answers

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.

like image 88
Stan1k Avatar answered Dec 20 '25 11:12

Stan1k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!