0

I want to change an image in WinForms based on a result.

if(x == true)
{
 // Show image 1
else
{
 // show image 2 

I saw that I can use Image.Fromfile("Image path"). But I have to use an image path something like this @"C:\test\test1.jpg".

So, is there any way that I can give a dynamic file path?

For example, if I open the project from another computer, this file path might change.

Thank you!

jimas13
  • 597
  • 5
  • 19
Manoj_S
  • 13
  • 1
  • And how would you get the path at first? – Cid Jun 21 '21 at 07:16
  • It is just the location where I save the images. – Manoj_S Jun 21 '21 at 07:17
  • Is this image part of application or user will browse ? – Amit Verma Jun 21 '21 at 07:17
  • I saved these images locally in my computer – Manoj_S Jun 21 '21 at 07:19
  • how would you tell the program where the images are stored? At some point, *"where I saved the images"* won't be enough, else if your program can read minds – Cid Jun 21 '21 at 07:19
  • Okay, so there isn´t any way to give the file path without hard coding? – Manoj_S Jun 21 '21 at 07:25
  • 2
    How to give a dynamical file path to an image? Easy, just create a variable, assign the desired value to it, and use it as the path to the image. You should focus in your question to the real problem whatever it is. Distributing files with your application? Getting the path of the executable? I would advice editing your question and providing more details on the actual problem. – Cleptus Jun 21 '21 at 07:25
  • There are a number of options you could try in the below post. https://stackoverflow.com/questions/24764353/load-picturebox-image-in-c-sharp-from-file-in-relative-path have you checked the above post? – jimas13 Jun 21 '21 at 13:07

1 Answers1

0

If you are using a PictureBox control set the ImageLocation property with the path, example:

pictureBox1.ImageLocation = @"C:\test\test1.jpg"