Skip to main content

Microsoft Silverlight

Answered Question Cannot get image dimensions with Image control!RSS Feed

(0)

Wilfred Pinto
Wilfred ...

Participant

Participant

1318 points

258 Posts

Cannot get image dimensions with Image control!

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 -

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" MaxWidth="300"/>
    </Grid>
</UserControl>

The code beside:

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.

Wilfred Pinto
http://projectsilverlight.blogspot.com


Please remember to mark the replies as answers if they help answer your question.

Wilfred Pinto
Wilfred ...

Participant

Participant

1318 points

258 Posts

Re: Cannot get image dimensions with Image control!

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 Pinto
http://projectsilverlight.blogspot.com
 

Wilfred Pinto
http://projectsilverlight.blogspot.com


Please remember to mark the replies as answers if they help answer your question.

Wilfred Pinto
Wilfred ...

Participant

Participant

1318 points

258 Posts

Answered Question

Re: Cannot get image dimensions with Image control!

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
                }
            };
 

Wilfred Pinto
http://projectsilverlight.blogspot.com
 


 

Wilfred Pinto
http://projectsilverlight.blogspot.com


Please remember to mark the replies as answers if they help answer your question.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities