Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BitmapImage reports PixelWidth & PixelHeight as zero. #10149

Open
Gavin-Williams opened this issue Nov 11, 2024 · 4 comments
Open

BitmapImage reports PixelWidth & PixelHeight as zero. #10149

Gavin-Williams opened this issue Nov 11, 2024 · 4 comments
Labels
bug Something isn't working needs-triage Issue needs to be triaged by the area owners

Comments

@Gavin-Williams
Copy link

Describe the bug

BitmapImage reports PixelWidth & PixelHeight as zero.

Steps to reproduce the bug

This seems to be a permanent issue.

Expected behavior

I expect PixelWidth & PixelHeight to report the size as presented.

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.6.1: 1.6.240923002

Windows version

Windows 11 (22H2): Build 22621

Additional context

No response

@Gavin-Williams Gavin-Williams added the bug Something isn't working label Nov 11, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Nov 11, 2024
@castorix
Copy link

The image must have been loaded.
For example when it is assigned to an Image control Source :

            BitmapImage bi = new BitmapImage(new Uri("ms-appx:///Assets/Butterfly.png"));
            bi.ImageOpened += (s, e) =>       
            {
                
                var nWidth = bi.PixelWidth;
                var nHeight = bi.PixelHeight;
                Debug.WriteLine($"Image size: {nWidth}x{nHeight}");
            };
            
            // ImageOpened will be called
            img1.Source = bi;

@Gavin-Williams
Copy link
Author

Gavin-Williams commented Nov 12, 2024

@castorix I don't see how this is different to my usage. But after I set a BitmapImage as source, the PixelWidth and PixelHeight are still zero.

if (!PieceImages.TryGetValue(pac.Value, out BitmapImage pieceBitmap))
    Debugger.Break();

Image pieceImage = new()
{
    Source = pieceBitmap,
    CanDrag = true
};

Debug.WriteLine(pieceBitmap.PixelWidth);
Debugger.Break();

Is it the case that UIElement resources are not built untill they are added to the live tree? ... No, I just tested, and even setting the BitmapImage as source to a live Image, as you do, does not set the Width & Height.

Update: With further thought. I guess the tree isn't actually live, just because I've declared it in Xaml, and set the source. The tree must be loaded later on.

@Gavin-Williams
Copy link
Author

Gavin-Williams commented Nov 12, 2024

Wow, the way they've done it seems pretty complicated to me. The image meta-data tells you the size of the image before you even load it, or the image object itself when built. That is a dependable value and always available. Having an event as the only way to access it makes it complicated. For example. I just wrote some code that uses the ImageOpened events to check image sizes. And the events don't ever trigger. So ... have they already triggered before I subscribe to the event. This seems like unnecessary complication. How do we subscribe to the events before they trigger? I've already got the code in the MainWindow constructor.

Update: Because the images I'm checking are drag and drop, they don't actually get built until the very moment the drag operation starts. Arrrrgh. I just want to check the image sizes when the application starts, not piecemeal at random other times.

Doesn't it seem just so simple to load the images and make their sizes available when I construct the images?

The simplification I can make in regard to drag-n-drop is that when the object is picked up, I can get the size at that moment and store the value globally since there is only a single object being dragged at a time. - Update - nope, that won't work, because the ImageOpened event only triggers the first time that particular image is opened. So I would have to record separate sizes for every image.

The ImageOpened event triggers after DragStarting event - where I need the image size. That's a catch 22 - and not a great approach to API design - what am I not understanding?

@Skittles2519
Copy link

@Gavin-Williams I'm not 100% sure how your project is set up and when/where you need the values, but with you mentioning needing the size with a DragStarting event, you can get the pixel values in that event as follows.

private void Image_DragStarting(UIElement sender, DragStartingEventArgs args) { var image = sender as Image; var bitmapimage = image.Source as BitmapImage; var pixelheight = bitmapimage.PixelHeight; var pixelwidth = bitmapimage.PixelWidth; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs-triage Issue needs to be triaged by the area owners
Projects
None yet
Development

No branches or pull requests

3 participants