Load images to Image from path outside of project

mastiSoft
mastiSoft used Ask the Experts™
on
Hi , I hope some one can help me.
I have somewhere in computer folder with .png images.
I need to show those images in Images containers in grid in MainWindow.xaml.  from code.
Right now I can only load images saved in project directory and cannot find how to use path to images to load them as source
There is not an option to move those images to project directory.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Are you trying to build an image gallery or something similar? You can use image path directly to load the images in the image control.

Author

Commented:
I don't know if I can call it is gallery. I need to show recent open files as images using path to folder where small images was created when file was loaded to application.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Perfect. All you need is programmatically get the path of the folder build a list<string> and bind it to a list of image controls data source.
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Author

Commented:
I added image to grid and named it as RecF1.  I have path to the .png file  ImagPathRecFiles.
 RecF1.Source =new Uri(@"ImagPathRecFiles", UriKind.Relative); gives error
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi mastiSoft,

I think there is something else is going on. And please :) If you save it gives an error - I will have to be omniscient to know what error it is giving. Always give the details on error. You are free to scrub any sensitive information for example, class name, namespace, credentials etc.

Alright, so I tried using declarative path
<Image Source="C:\Users\Chinmay\Desktop\about.png" HorizontalAlignment="Stretch" 
    Stretch="Fill" VerticalAlignment="Stretch" Grid.Row="0" Grid.RowSpan="5" />

Open in new window


And the result is:
WPF Image Source - Non-project path
I think you should try UrlKind.Absolute, Relative will search for the image basis your App's root folder.

Regards,
Chinmay.

Author

Commented:
thank you for trying to help. I don't have problems to access source of image from xaml, what I try is do it from code
Chief Technology Ninja
Distinguished Expert 2018
Commented:
In that case,
this.BgImage.Source = new BitmapImage(new Uri((@"C:\Users\Chinmay\Desktop\about.png")));

Open in new window

Author

Commented:
thank you

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