简体   繁体   中英

BitmapImage Stretch mode to Image tag in xaml

I am using in xaml with some fix image source, later on I wanted to use local image and wanted to assign to the xaml. But I am not able to use stretch in new image created.

Is there any way to use the stretch in other way? My code in xaml:

 <Image Source="/Assets/Images/fix.png" Visibility="Visible" x:Name="ContentImage" Stretch="UniformToFill" />

and in C#

if (File.Exists(imagePath))
                        {
                            Image newImage = new Image();
                            BitmapImage logo = new BitmapImage();
                            logo.BeginInit();
                            logo.UriSource = new Uri(imagePath);                                
                            logo.EndInit();
                            newImage.Source = logo;
                            newImage.Stretch = Stretch.UniformToFill;
                            ContentImage.Source = newImage.Source;
                        }

It makes no sense to create another Image element. It is also not necessary to call Begin/EndInit. Use the BitmapImage constructor that takes an Uri argument instead.

This should be sufficient:

if (File.Exists(imagePath))
{
    ContentImage.Source = new BitmapImage(new Uri(imagePath));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM