How to get Silverlight's bitmapimage pixel height before it is drawn to canvas?
-
28-09-2019 - |
문제
I have an Image, which I need to scale based on the image's pixel values. When I create the bitmapImage, the pixels (height and width) are both 0.
Is there a way to get height and width before the image is drawn to the canvas?
해결책
I had a similar issue where I needed to measure the height of a row (with content) being added to a Grid before adding it to the Grid. Short answer - it can't be done.
Long answer - it can be done. In my code-behind, I created a dummy Grid not a part of the UI. I would add the row to it, and then call .Measure on the dummy Grid that would give me the rendered size of the row. Then I could just add the row to the actual Grid on the UI.
I suspect you could try the same approach with your image. Create a dummy Canvas, add the image to the Canvas, and see how big it is.