Wuceng
Topic Author
Posts: 18
Joined: 04 Aug 2024, 16:43

Bitmap Image GetWidth or GetPixelWidth always returns 0

27 Jan 2025, 21:02

Hey! I'm loading an image from code using a BitmapImage and trying to read its width and height.
For some reason those are always returning 0 for me but I would expect to get the size of the image in pixels (at least with GetPixelWidth).
The image can be used and displays perfectly fine when using it for a brush later in the code so I doubt its the image being corrupt or not found.
The same is also happening for the height.
What am I missing here, or is this a bug?

minimal repro:
const auto image = Noesis::MakePtr<Noesis::BitmapImage>(Noesis::Uri("Path/To/Image/image.png"));
const int pixelWidth = image->GetPixelWidth(); // Will always return 0
const int width = image->GetWidth(); // Will always return 0
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: Bitmap Image GetWidth or GetPixelWidth always returns 0

30 Jan 2025, 13:05

Hi,

Newly created resources are not initialized and haven't loaded the required asset to get the information. If you call Init on the image it will load the specified png and you should be able to get its size information:
const auto image = Noesis::MakePtr<Noesis::BitmapImage>(Noesis::Uri("Path/To/Image/image.png"));
image->Init();
const int pixelWidth = image->GetPixelWidth();
const int width = image->GetWidth();
Could you try this?
 
Wuceng
Topic Author
Posts: 18
Joined: 04 Aug 2024, 16:43

Re: Bitmap Image GetWidth or GetPixelWidth always returns 0

30 Jan 2025, 22:59

That indeed works. Thanks a lot!

Could you give me some context maybe for what Init does in the case of a Bitmap (I assume it loads it into memory?) and point me to some documentation about Init in general?
I wasn't really sure how I could have figured this out on my own.
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: Bitmap Image GetWidth or GetPixelWidth always returns 0

04 Feb 2025, 11:23

In the case of the BitmapImage, when Init is called we use the TextureProvider to access the image file and read the header for getting the bitmap information.

In general the Init() method just puts DependencyObjects in a ready state to listen and notify of property changes. In normal circumstances you don't have to bother about calling this method because all objects and resources are automatically initialized when added to the View.

Who is online

Users browsing this forum: No registered users and 10 guests