WPF Image Management Source

im trying to recreate a very simple example of a C # project I am a WPF project, its a simple image viewer .. from C # teaches itself, ive managed to open an open file dialog, but how to set the image path to the image.source control in WPF?

private void SearchBtn_Click(object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog openfile = new Microsoft.Win32.OpenFileDialog(); openfile.DefaultExt = "*.jpg"; openfile.Filter = "Image Files|*.jpg"; Nullable<bool> result = openfile.ShowDialog(); if (result == true) { //imagebox.Source = openfile.FileName; } } 
+7
c # image wpf-controls
source share
3 answers
 imagebox.Source = new BitmapImage(new Uri(openfile.FileName)); 
+18
source share

you will need to change the file name in the URI and then create bitmapimage

:

 if (File.Exists(openfile.FileName)) { // Create image element to set as icon on the menu element BitmapImage bmImage = new BitmapImage(); bmImage.BeginInit(); bmImage.UriSource = new Uri(openfile.FileName, UriKind.Absolute); bmImage.EndInit(); // imagebox.Source = bmImage; } 
+3
source share

You can also add an image as a resource, i.e. add an existing item and change the Build Action property for the resource

then link to it that way

 BitmapImage bitImg = new BitmapImage(); bitImg.BeginInit(); bitImg.UriSource = new Uri("./Resource/Images/Bar1.png", UriKind.Relative); bitImg.EndInit(); ((Image)sender).Source = bitImg; 

Thus, you do not need to include an image with the program, it is included in the package as a resource

+2
source share

All Articles