3

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?

Stan1k
  • 338
  • 2
  • 17
Jermartyno
  • 43
  • 1
  • 7
  • When and for what purpose do you do it? The folder structure at the time of development project does not cooperate with the time of execution. If you want to get the folder location in the source code of your program, you need to design the folder structure after installation and deployment and code accordingly. It is also possible to build the configuration during debugging, or to give information from the outside using a configuration file or the like. – kunif Mar 24 '21 at 09:56
  • Provide some code, which you use for access image. And what do you mean by access image? Do you want image path or read image ? – patel vinay Mar 24 '21 at 10:01
  • Otherwise, these articles will be helpful for project settings such as builds. [Set compiler and build properties](https://learn.microsoft.com/en-us/cpp/build/working-with-project-properties?view=msvc-160), [Common macros for MSBuild commands and properties](https://learn.microsoft.com/en-us/cpp/build/reference/common-macros-for-build-commands-and-properties?view=msvc-160) – kunif Mar 24 '21 at 10:02
  • Refer this link https://stackoverflow.com/questions/6041332/best-way-to-get-application-folder-path . hope it's help. – patel vinay Mar 24 '21 at 10:09

2 Answers2

2

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.

Stan1k
  • 338
  • 2
  • 17
1

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.

Jermartyno
  • 43
  • 1
  • 7