Bitmap Image GetWidth or GetPixelWidth always returns 0
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:
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:
Code: Select all
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
-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: Bitmap Image GetWidth or GetPixelWidth always returns 0
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:
Could you try this?
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:
Code: Select all
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();
Re: Bitmap Image GetWidth or GetPixelWidth always returns 0
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.
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.
-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: Bitmap Image GetWidth or GetPixelWidth always returns 0
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.
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