Skip to main content
Home Forums Silverlight Programming Report a Silverlight Bug Cannot get image dimensions with Image control!
2 replies. Latest Post by Wilfred Pinto on April 25, 2008.
(0)
Wilfred ...
Participant
1306 points
256 Posts
04-24-2008 3:54 PM |
This has been covered before but I am not sure if an official bug has been reported. I need this functionality and I am hoping it will be fixed by the next release (Beta 2?)!
The Xaml:
<UserControl x:Class="Test.Page" xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="100" Height="300"> <Grid x:Name="LayoutRoot" Background="Black"> <Image x:Name="MyImage" Source="/images/yellowlily1.jpg" MaxWidth="300"/> </Grid></UserControl>
The code beside:
public Page(){ InitializeComponent(); MyImage.MouseLeftButtonDown += (sender, e) => { HtmlPage.Window.Alert("MouseLeftButtonDown" + ": " + MyImage.Height.ToString() + ":" + MyImage.MaxWidth.ToString() + ":" + MyImage.ActualHeight.ToString() + ":" + MyImage.ActualWidth.ToString() + ":" + MyImage.RenderSize.ToString()); }; MyImage.Loaded += (sender, e) => { HtmlPage.Window.Alert("Loaded" + ": " + MyImage.Height.ToString() + ":" + MyImage.MaxWidth.ToString() + ":" + MyImage.ActualHeight.ToString() + ":" + MyImage.ActualWidth.ToString() + ":" + MyImage.RenderSize.ToString()); };}The result:
Loaded: Nan:300:0:0:0,0 MouseLeftButtonDown: 66:300:66:88:88,66
There doesn't seem to be a way of getting the image dimensions on image load. Occasionally the Loaded event will give the right dimensions.
I also tried BitmapImage class -
<UserControl x:Class="Test.Page" xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="100" Height="300"> <Grid x:Name="LayoutRoot" Background="Black"> <Image x:Name="MyImage" MaxWidth="300"/> </Grid></UserControl>
public Page(){ InitializeComponent(); BitmapImage bi = new BitmapImage(); bi.UriSource = new Uri("/images/yellowlily1.jpg", UriKind.Relative); bi.DownloadProgress += (sender, e) => { if(e.Progress == 1) HtmlPage.Window.Alert("DownloadProgress:" + ": " + MyImage.Height.ToString() + ":" + MyImage.MaxWidth.ToString() + ":" + MyImage.ActualHeight.ToString() + ":" + MyImage.ActualWidth.ToString() + ":" + MyImage.RenderSize.ToString()); }; MyImage.MouseLeftButtonDown += (sender, e) => { HtmlPage.Window.Alert("MouseLeftButtonDown" + ": " + MyImage.Height.ToString() + ":" + MyImage.MaxWidth.ToString() + ":" + MyImage.ActualHeight.ToString() + ":" + MyImage.ActualWidth.ToString() + ":" + MyImage.RenderSize.ToString()); }; MyImage.Loaded += (sender, e) => { HtmlPage.Window.Alert("Loaded" + ": " + MyImage.Height.ToString() + ":" + MyImage.MaxWidth.ToString() + ":" + MyImage.ActualHeight.ToString() + ":" + MyImage.ActualWidth.ToString() + ":" + MyImage.RenderSize.ToString()); }; MyImage.Source = bi;}
The result:
DownloadProgress: Nan:300:0:0:0,0
What is weird here is that the downloadprogress event is called 3 times for e.Progress == 1
All this is executing on the local machine (no server).
----------
All I want to do is put a simple border around the image. It doesn't seem possible since I cannot get the dimensions of the image. If there is a workaround for this, I would love to hear it! Note that I cannot specify the dimensions for the image or grid control since I want the image control to intelligently render the image.
04-24-2008 5:10 PM |
A Kludgey workaround
internal void DrawBorderAroundImage(){ DispatcherTimer dispatcherTimer = new DispatcherTimer(); dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 100); dispatcherTimer.Tick += (sender, e) => { ((DispatcherTimer)sender).Stop(); HtmlPage.Window.Alert(MyImage.Height.ToString() + ":" + MyImage.MaxWidth.ToString() + ":" + MyImage.ActualHeight.ToString() + ":" + MyImage.ActualWidth.ToString() + ":" + MyImage.RenderSize.ToString()); }; }
This may / may not work since 100 msec is an approximation. But it illustrates the problem!
Please fix it soon.
Thanks
Wilfred Pintohttp://projectsilverlight.blogspot.com
04-25-2008 1:55 PM |
Ok... found a nice workaround, so I thought I'd post it here. It seems to work well but if anyone discovers any issue please post a follow up to this post.
Handle the Image.SizeChanged event but take the image dimensions like this
MyImage.SizeChanged += (sender, e) => { if (e.PreviousSize.Width == 0 && e.PreviousSize.Height == 0 && e.NewSize != e.PreviousSize) { // e.NewSize contains the size of the image } };