Reputation: 1661
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
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
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
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