1

I am making an application where users can make an account and add a profile image. In my Main_Page I have an Image in the Main_Page.xaml.cs I am setting Image.Source = image. image is a BitmapImage. I get no compile errors so the image should be displayed. I did a watch on the path to the image and its correct! I will include code for reference.

 public Main_Page()
    {

        BitmapImage image = new BitmapImage(new Uri(Environment.CurrentDirectory + @"\Images\" + ApplicationServices.User.XMLFile.SelectSingleNode(@"User/ProfilePicture").InnerText.Trim(), UriKind.Relative));
        Profile_Image_Exercise.Source = image;
        Profile_Image_Nutrition.Source = image;
        Profile_Image_Statistics.Source = image;

    }

@EdPlunkett made a good point im new to WPF so could this be causing my issues? enter image description here

Alvaromon
  • 190
  • 2
  • 16
  • 1
    Why is your Uri relative if you give it an absolute path? – Etienne de Martel Jun 18 '17 at 01:03
  • *"I get no compile errors so the image should be displayed"* -- nope. What path are you really passing to the Uri constructor? I'm not asking what you imagine it is. What *actual string at runtime?* Use the debugger to find out. Does that file exist? Is it what you think it is? – 15ee8f99-57ff-4f92-890c-b56153 Jun 18 '17 at 01:04
  • Are Profile_Image_Exercise etc actually visible in the ui? Perhaps not, since you didn't show us any XAML. Did you look at the VS output pane at runtime for errors? Give it a try. Is the above snippet of code even being executed? Don't *assume*; use the debugger to find out. Are you eating exceptions in an empty catch block? – 15ee8f99-57ff-4f92-890c-b56153 Jun 18 '17 at 01:08
  • 1
    @EdPlunkett yes it exists. the image is visible in the UI. not eating any exceptions. no runtime errors either. The actual string at runtime is, for example, in my profile: "C:\\Users\\alvaro\\Documents\\GitHub\\TrackFit\\TrackFit\\TrackFit Project\\bin\\Debug\\Images\\Zelda.jpg" – Alvaromon Jun 18 '17 at 01:12
  • 1
    Look at the `Source` URI. As you can see it is a pack URI (pack://) so your image must be added to project as a resource. It is because you use `UriKind.Relative`. `UriKind.Absolute` gives you file URI (file:///) and image will be shown without any problems. See [this](https://stackoverflow.com/questions/10654861/image-source-urikind) topic. – Maxim Jun 18 '17 at 02:31
  • @Maxim THANK YOU the answer was in front of my face all along. Occum's razor i guess – Alvaromon Jun 18 '17 at 02:50
  • @EtiennedeMartel THANK YOU also since you were the first one to mention this and i over looked it ! – Alvaromon Jun 18 '17 at 02:50

0 Answers0