Trey Balut
Trey Balut

Reputation: 1395

.NET Maui Load Image

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.

Upvotes: 3

Views: 10562

Answers (3)

Jessie Zhang -MSFT
Jessie Zhang -MSFT

Reputation: 13949

If you want to load an embedded image, please set the Build Action to Embedded resource

And also refer to the following code:

ImageView1.Source =   ImageSource.FromResource("CWON22.Resources.Images.test.png");

For more information, check : Load an embedded image .

Upvotes: 0

Hashim Shubber
Hashim Shubber

Reputation: 9

Try this:

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

Upvotes: 0

ToolmakerSteve
ToolmakerSteve

Reputation: 21482

Try:

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

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

Upvotes: 1

Related Questions