locked
ImageOpened event fired, PixelWidth and PixelHeight remain 0 RRS feed

  • Question

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

    Saturday, August 10, 2013 4:24 PM

Answers

  • Hi Peping,

    I suspect the problem is that you are setting the wrong Background property rather than an issue with the bitmap sizes.

    PixelWidth and PixelHeight should be set in the ImageOpened handler, and when I put your code in a project and call OpenBitmap from either OnNavigatedTo or from a Button.Click handler the PixelWidth and PixelHeight are set where you comment that they are 0.

    In most apps (including the default templates) the page's Background is not visible but is covered by a Panel object such as a Grid. If I change the Background = b call to change my Grid's Background instead of the Page's then it shows up.

     --Rob

    Sunday, August 11, 2013 1:34 AM
    Moderator
  • You see, that's not the problem - The image displays fine. I just can't get PixelWidth and PixelHeight when ImageOpened fires. I'm attaching a solution that replicates this behavior, at least on my machine:

    Oh! It was just me being silly. Instead of PixelWidth and PixelHeight, I was reading DecodePixelWidth and DecodePixelHeight.

    Sorry :)



    Sunday, August 11, 2013 8:51 AM

All replies

  • Hi Peping,

    I suspect the problem is that you are setting the wrong Background property rather than an issue with the bitmap sizes.

    PixelWidth and PixelHeight should be set in the ImageOpened handler, and when I put your code in a project and call OpenBitmap from either OnNavigatedTo or from a Button.Click handler the PixelWidth and PixelHeight are set where you comment that they are 0.

    In most apps (including the default templates) the page's Background is not visible but is covered by a Panel object such as a Grid. If I change the Background = b call to change my Grid's Background instead of the Page's then it shows up.

     --Rob

    Sunday, August 11, 2013 1:34 AM
    Moderator
  • You see, that's not the problem - The image displays fine. I just can't get PixelWidth and PixelHeight when ImageOpened fires. I'm attaching a solution that replicates this behavior, at least on my machine:

    Oh! It was just me being silly. Instead of PixelWidth and PixelHeight, I was reading DecodePixelWidth and DecodePixelHeight.

    Sorry :)



    Sunday, August 11, 2013 8:51 AM