It controls what happens when the dimensions of the image loaded doesn't completely match the dimensions of the Image control. Here's the XAML source, as well as a screenshot, of our Code-behind sample: Īfter the Source property, which is important for obvious reasons, I think the second most interesting property of the Image control might be the Stretch property. We use the same relative path as we used in one of the previous examples - just be sure to pass in the UriKind.Relative value when you create the Uri instance, so it knows that the path supplied is not an absolute path. ImgDynamic.Source = new BitmapImage(resourceUri) Uri resourceUri = new Uri("/Images/white_bengal_tiger.jpg", UriKind.Relative) We can use the exact same technique to load an image included in the application as a resource: private void BtnLoadFromResource_Click(object sender, RoutedEventArgs e) Notice how I create a BitmapImage instance, which I pass a Uri object to, based on the selected path from the dialog. ImgDynamic.Source = new BitmapImage(fileUri) Uri fileUri = new Uri(openFileDialog.FileName) OpenFileDialog openFileDialog = new OpenFileDialog() Here's how you can load an image found on the user's computer, based on their selection from an OpenFileDialog: private void BtnLoadFromFile_Click(object sender, RoutedEventArgs e) Specifying the Image Source directly in your XAML will work out for a lot of cases, but sometimes you need to load an image dynamically, e.g. Using a simple relative URL, it would look like this: Loading images dynamically (Code-behind) To simplify things, the WPF framework will also accept a simple, relative URL - this will suffice in most cases, unless you're doing something more complicated in your application, in regards to resources. Using this syntax, you can easily reference resources included in your application.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |