im trying to recreate a very simple example of a C# project i WPF, its a simple image viewer.. from the sam's teach yourself C#, ive managed to get the open file dialog to open, but how do i 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;
}
}
There is a slight variation on this: <Image Source="/Icons/play_small. png" /> Note the forward slash at the start which means look in the root folder. If the xaml file is not at the root, this is necessary as without the forward slash, it will start the search in the same directory as the xaml file.
Here's how to set the source to an image from the app package. Image img = new Image(); BitmapImage bitmapImage = new BitmapImage(); Uri uri = new Uri("ms-appx:///Assets/Logo.png"); bitmapImage. UriSource = uri; img. Source = bitmapImage; // OR Image img = new Image(); img.
The TextBlock control provides flexible text support for UI scenarios that do not require more than one paragraph of text. It supports a number of properties that enable precise control of presentation, such as FontFamily, FontSize, FontWeight, TextEffects, and TextWrapping.
imagebox.Source = new BitmapImage(new Uri(openfile.FileName));
you'll need to change the File Name into a URI and then create a 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With