How can i point to a default image in my C# application

Olukayode Oluwole
Olukayode Oluwole used Ask the Experts™
on
I have a default image i want to use in C# application

I stored  the default image  in c:\crystal\images\defaultimage.png

I am trying to point to  it in my application as my default image but

somehow pointing  to the file with pbxPhoto.Image = ("C:\Crystal\Images\defaultimage.png");

 gives a syntax error (see attached)

Please note that i am getting the error in the code even before running

How can i resolve this error
ImagePath230319.PNG
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
what error you are getting ?
Olukayode OluwoleSystems Analyst

Author

Commented:
If you look at the attached file you will see the  slash character between the folders

being flagged.

This is more like a syntax error and of course i cant run with the error

Regards

Olukay
Commented:
The backslash is an escape character in C#.  You need to add either a string literal indicator at the front of the string:
pbxPhoto.Image = (@"C:\Crystal\Images\defaultimage.png");

Open in new window

Or, use an extra backslash -
pbxPhoto.Image = ("C:\\Crystal\\Images\\defaultimage.png");

Open in new window


That being said a better option would be using the image as an embedded resource; that is unless the user can choose a different image.

-saige-
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Top Expert 2016

Commented:
either use your appsettings or copy the image to your source directory and then use a relative path from there
https://www.youtube.com/watch?v=fzfztjaqR18
Olukayode OluwoleSystems Analyst

Author

Commented:
I have tried both options with the 2 statements

pbxPhoto.Image = Image.FromFile("C:\\Crystal\\Images\\defaultimage.png");
pbxPhoto.Image = Image.FromFile(@"C:\Crystal\Images\defaultimage.png");

I got the same error  File not Found (see attached)

Error Screen form the 2 Options provided
Even though the file exists.

What could I be doing wrong

Olukay
Olukayode OluwoleSystems Analyst

Author

Commented:
Thanks for the effort.
I would have loved to listen to the UTube video but there is no voice

Regards

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial