mobile app Beginner
mobile app Beginner

Reputation: 1661

Image size always return 0

I am trying to load a image from Embedded Resource to an Image instance. The problem is the size of the image is always 0.

Here is the code snippet:

Image image = new Image();
ImageSource img = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Images/marker.png", UriKind.Relative));
image.SetValue(Image.SourceProperty, img);


System.Diagnostics.Debug.WriteLine("output 1 = " + image.DesiredSize.Width); // return 0
System.Diagnostics.Debug.WriteLine("output 2 = " + image.ActualWidth); // return 0

I have to know the size of the image before it rendered on the screen, because I need to offset the image depending on the size of it.

Thank you



Update: Thanks Silvermind

I answered my own question with sample code

Upvotes: 0

Views: 360

Answers (3)

Joachim Isaksson
Joachim Isaksson

Reputation: 180867

Looking at the BitmapImage.DownloadProgress Event;

Constructing a BitmapImage by URI is inherently asynchronous. This event reports on progress of the construction.

In other words, running ActualWidth before the BitmapImage has necessarily been downloaded/constructed may very well return 0 as ActualWidth.

Upvotes: 2

mobile app Beginner
mobile app Beginner

Reputation: 1661

Thanks Silvermind. He suggested to use CreateOptions.

Finally, I found a solution:

Image image = new Image();

Uri uri = new Uri("/Images/marker_stop.png", UriKind.Relative);
BitmapImage bi = new BitmapImage(uri);
bi.CreateOptions = BitmapCreateOptions.IgnoreImageCache;

image.Source = bi;
System.Diagnostics.Debug.WriteLine("************************************* in image.ActualWidth " + ", " + bi.PixelWidth); // return 30

Upvotes: 0

Rustam Kichinsky
Rustam Kichinsky

Reputation: 129

"/Images/marker.png" - are your sure that here is the file?

maybe it is because the folder is named images without the capital I?

also what returns the:

image.getValue(); // here what null? after setting it

Upvotes: 1

Related Questions