Home > Software design >  .NET Maui Load Image
.NET Maui Load Image

Time:08-27

I'm trying to load an image in my code behind. I can successfully load an image into an Image control using xaml, but in my xaml.cs file nothing appears.

Here is my xaml:

   <Image Grid.Row="1" Grid.RowSpan="1" Grid.Column="0"  x:Name="ImageView1"  HorizontalOptions="Start" VerticalOptions="CenterAndExpand" Source="alpha.jpg" />

The image from "Resources/Images/alpha.jpg" is loaded. The Build Content is MauiImage.

But here is the code in the mainpage.xaml.cs file.

   ImageView1.Source = "CWON22.Resources.Images.beta.jpg";

The Image Control, ImageView1 is blank.

CodePudding user response:

Try this:

ImageView1.Source = new BitmapImage(new Uri(@CWON22.Resources.Images.beta.jpg, UriKind.RelativeOrAbsolute));

CodePudding user response:

Try:

ImageView1.Source = new ImageSource.FromResource("beta.jpg");

// OR
ImageView1.Source = new ImageSource.FromResource("Resources/Images/beta.jpg");
  • Related