MZ Determine width and height of loaded bitmap

Hi, I'm writing a RMMZ plugin that extends Sprite and loads a bitmap into the sprite.

I can load an image into the sprites bitmap with:

JavaScript:
this.bitmap=Bitmap.load("/img/pictures/msgimg_0.png");

However although the image loads this.bitmap.width and this.bitmap.height are zero.
I can get the image to display using something like:

JavaScript:
this.bitmap.resize(Graphics.width,Graphics.height);

However I need to know what the actual width and height of the .png file I am loading is through code.
I could possibly use Bitmap.getPixel or Bitmap.getAlphaPixel to identify the edges of the actual image, but my hope is an easier way exists.

Any ideas how I can dynamically obtain the width and height of the image I am loading?
You probably need to wait until the bitmap is loaded with bitmap.isReady().

The next question would be, is it really necessary to know the bitmap dimensions? As far as I know, you can create a Sprite like this const sprite = new Sprite(bitmap) and the Sprite class handles everything for you.

Do you mind showing us a little bit more of your coding?
Images are loaded asynchronously. You can wait for it to load using a load listener, e.g.
JavaScript:
ImageManager.loadPicture("msgimg_0").addLoadListener(
  bmp => console.log(bmp.width, bmp.height)
);
Alternatively, if the image is requested in a scene's create method, the scene will only start once the ImageManager has completed all requests.

Like Aerosys says, a Bitmap simply contains image data; you'd want a Sprite of some kind for display purposes.

[Edit: wait, do you mean the sprite isn't updating its appearance accordingly? Because it should.]
Images are loaded asynchronously. You can wait for it to load using a load listener, e.g.
JavaScript:
ImageManager.loadPicture("msgimg_0").addLoadListener(
  bmp => console.log(bmp.width, bmp.height)
);
Alternatively, if the image is requested in a scene's create method, the scene will only start once the ImageManager has completed all requests.

Like Aerosys says, a Bitmap simply contains image data; you'd want a Sprite of some kind for display purposes.

[Edit: wait, do you mean the sprite isn't updating its appearance accordingly? Because it should.]
Thanks for the quick responses guys. Caethyril, the load listener was the perfect solution. I had the sprite displaying on the screen fine, it's just I want to dynamically place the image based on the current resolution (which is also dynamic because I'm using a plugin that allows the user to change the resolution). Now I have the width and height, I can calculate the exact screen position correctly. Thanks for the help.
Images are loaded asynchronously. You can wait for it to load using a load listener, e.g.
JavaScript:
ImageManager.loadPicture("msgimg_0").addLoadListener(
  bmp => console.log(bmp.width, bmp.height)
);
Alternatively, if the image is requested in a scene's create method, the scene will only start once the ImageManager has completed all requests.

Like Aerosys says, a Bitmap simply contains image data; you'd want a Sprite of some kind for display purposes.

[Edit: wait, do you mean the sprite isn't updating its appearance accordingly? Because it should.]
Hi caethyril, based on searching, you seem to be the resident expert on image manipulation. ☺️ I have a different area of my plugin that has a variant of the same issue. I'm not loading the bitmap in this case, I'm creating a new one.

Can I use addLoadListener directly on my new bitmap?

Reason I ask is that further down in my code I'm using Bitmap.getPixel which is returning zero regardless of which pixel I check. I suspect it's the same kind of problem, where the bitmap isn't loaded yet.

I don't have the exact code to hand, but this is an example of what I'm talking about:
JavaScript:
let myBitmap=new (100,100);
myBitmap.fillAll("#fc0000");
let myValue =  MyBitmap.getPixel(5,5);

I would expect myValue to be "#fc0000" except it isn't it's always zero.
Attaching a load listener will automatically run that listener if the bitmap is not currently loading (cf Bitmap#addLoadListener, rmmz_core.js).

Load listeners are generally only needed if loading from a URL, typically via Bitmap.load. The Bitmap#fillAll method uses CanvasRenderingContext2D#fillRect; since that is synchronous, there should be no problem getting pixel data immediately.

I notice you have a couple of typos in that example: new (100, 100) (missing Bitmap) and MyBitmap.getPixel (should be myBitmap).

Here's an example I just tested via the console (F8 or F12 during playtest):
JavaScript:
const b = new Bitmap(100, 100);
b.fillAll("#8800ff");
console.log("immediate", b.getPixel(5, 5));
b.addLoadListener(bmp => console.log("listener", bmp.getPixel(5, 5)));
Both console logs report the same, correct colour for me:

immediate #8800ff listener #8800ff

Original thread: https://forums.rpgmakerweb.com/threads/determine-width-and-height-of-loaded-bitmap.165863/