Click here to Skip to main content
16,015,504 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
var img = ds.Tables[0].Rows[0][11].ToString();
                       string location = ".. / .. / .. / .. / Restaurant_Application/image.png";
                       string path = System.IO.Path.Combine(location, picUserimage.Source + ".jpg");
                       BitmapImage myBitmapImage = new BitmapImage(new Uri(path + img, UriKind.Relative));
                       myBitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                       picUserimage.Source = myBitmapImage;


What I have tried:

var img = ds.Tables[0].Rows[0][11].ToString();
                       string location = ".. / .. / .. / .. / Restaurant_Application/image.png";
                       string path = System.IO.Path.Combine(location, picUserimage.Source + ".jpg");
                       BitmapImage myBitmapImage = new BitmapImage(new Uri(path + img, UriKind.Relative));
                       myBitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                       picUserimage.Source = myBitmapImage;
Posted
Updated 14-Sep-22 2:34am
Comments
Sandeep Mewara 14-Sep-22 8:08am    
Your query is not clear - are you looking for something like: https://www.c-sharpcorner.com/UploadFile/mahesh/using-xaml-image-in-wpf/

1 solution

You're not "converting a string". You're building a path to a file, and I don't think you're doing it correctly. You're path is jumping up 4 directory levels, then going down one into a " Restaurant_Application" directory (note the space on the beginning of the name) to find the file. Your code is assuming that's where the file will always be relative to the "current directory". Never use .. in path names to jump between folders. The "current directory" may change and your code will not know to change that path. ALWAYS build a path to a file based on a well-known root path.

Look into Environment.GetFolderPath[^] and Environment.SpecialFolder Enum[^] to get the well-known path to start with. You can then use Path.Combine to append folders to that path to build the path to the file you want to get at.

Oh, and why are you putting a bunch of spaces in the path? Remove them. Yes, they matter and CAN impact the validity of the path you build.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900