1

I have an Image control, generated in runtime. It renders correct size when displaying. But I can't get size of that element. Width and Height is NaN. ActualWidth and ActualHeight is always 0.0 and control never fires SizeChanged event.

enter image description here

I'm also generating TextBoxes in the runtime and I can't get size of them too.

The code that generates image is bellow.

ExtendedImage image = new ExtendedImage();
image.Name = "element" + item.Id;
if (item.Text.ToUpperInvariant().EndsWith(".GIF"))
{
    var gif = BookReader.Imaging.GIFDecoder.Decode(Application.GetResourceStream(new Uri("Files/Books/" + item.BookId + "/" + item.Text, UriKind.Relative)).Stream);
    image.Source = gif.Frames[0].Image;
}
else
{
    var img = new BitmapImage(new Uri("/Files/Books/" + item.BookId + "/" + item.Text, UriKind.Relative));
    image.Source = img;
}

image.SetValue(Canvas.LeftProperty, (double)item.LocationX);
image.SetValue(Canvas.TopProperty, (double)(Application.Current.Host.Content.ActualHeight - item.LocationY));

image.SizeChanged += (o, e) =>
{
    var sender = o as ExtendedImage;
    image.SetValue(UIElement.RenderTransformOriginProperty, new Point(0.5, 0.5));
};

image.InvalidateMeasure();

if (!item.Visible)
{
    image.Visibility = System.Windows.Visibility.Collapsed;
}

ContentPanel.Children.Add(image);
Medeni Baykal
  • 4,223
  • 1
  • 28
  • 36

2 Answers2

3

You can't access the size of the image before it's been rendered. And you can't render a item in a Canvas, without it having a explicit size.

So your problem is that you're attempting to get the size of the image before it's actually been rendered.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
1

If the container of the Image has Height and Width, the image should have it as well. Try to put your Image into a Grid or StackPanel and set the size explicitly.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Lukasz Madon
  • 14,664
  • 14
  • 64
  • 108