Friday, 9 August 2013

ImageOpened event fired, PixelWidth and PixelHeight remain 0

ImageOpened event fired, PixelWidth and PixelHeight remain 0

When trying to determine a bitmap's size right after they are loaded, I
ran into a problem. The BitmapImage's PixelWidth and PixelHeight remain 0
as if the image is not loaded yet when the ImageOpened event is fired.
Though, the image displays as it should.
This is what I do:
protected async void OpenBitmap()
{
BitmapImage img = new BitmapImage();
img.ImageOpened += SetBackground;
StorageFile file = await
StorageFile.GetFileFromApplicationUriAsync(new
Uri("ms-appx:///Assets/SplashScreen.png",UriKind.Absolute));
await img.SetSourceAsync(await file.OpenReadAsync());
}
protected void SetBackground(object sender, RoutedEventArgs args)
{
ImageBrush b = new ImageBrush();
// Breakpoint here suggests that sender.PixelWidth and
sender.PixelHeight are 0
b.ImageSource = sender as BitmapImage;
Background = b;
}

No comments:

Post a Comment