MZ Merge text and image bitmaps

Good day,

I'm trying to put text on image and execute the following code:

JavaScript:
function createTextPicture(text) {
        const tempWindow = new Window_Base(new Rectangle());
        const size = tempWindow.textSizeEx(text);
        tempWindow.padding = 0;
        tempWindow.move(0, 0, 500,500);
        tempWindow.createContents();       
        
        let img = ImageManager.loadPicture('1');
        const pw = img.width;
        const ph = img.height;
        tempWindow.contents.blt(img, pw, ph, pw, ph, pw, ph);

        tempWindow.drawTextEx(text, 400, 400, 0);
        
        const bitmap = tempWindow.contents;
        tempWindow.contents = null;
        tempWindow.destroy();
        bitmap.mzkp_isTextPicture = true;
        return bitmap;
    }

loadPicture() returns the bitmap, and I see that contents.blt() should modify the window bitmap, but for some reason drawTextEx overwrites the changes.
What am I doing wrong? Maybe is there anybody solved a similar problem?

Thank you!
for some reason drawTextEx overwrites the changes.
So, you're trying to create a new bitmap that has both the image and text? What are you getting instead? Does this work without drawTextEx (returning just a bitmap without the text but with the image)?
So, you're trying to create a new bitmap that has both the image and text? What are you getting instead? Does this work without drawTextEx (returning just a bitmap without the text but with the image)?
>So, you're trying to create a new bitmap that has both the image and text?
Correct.

>Does this work without drawTextEx (returning just a bitmap without the text but with the image)?
No.
Hi there!

The picture could be above the text in some cases because it can takes time for it to load. This could also causes the img.width and img.height to return 0 (or undefined, don't remember).

After you load the picture, add a listener to it, and then put everything between the let img and return inside that listener function. This will make that the game inly draws the text after the picture is loaded. Therefore, will draw above the picture.

Now I did not understand why are you destroying the window and null it's contents.
>Does this work without drawTextEx (returning just a bitmap without the text but with the image)?
No.
Right, so it's not really drawTextEx that's overwriting anything here, if it still happens with it gone.

One thing that stands out to me is your blt arguments - you're setting the starting point to be equal to the image's width and height, so this isn't actually going to copy any part of the image. And you're doing the same with the destination, which is not necessarily wrong, but it might render that image outside of the bitmap depending on their relative dimensions. Here are the arguments that function is expecting:
JavaScript:
/**
 * Performs a block transfer.
 *
 * @method blt
 * @param {Bitmap} source The bitmap to draw
 * @param {Number} sx The x coordinate in the source
 * @param {Number} sy The y coordinate in the source
 * @param {Number} sw The width of the source image
 * @param {Number} sh The height of the source image
 * @param {Number} dx The x coordinate in the destination
 * @param {Number} dy The y coordinate in the destination
 * @param {Number} [dw=sw] The width to draw the image in the destination
 * @param {Number} [dh=sh] The height to draw the image in the destination
 */
Hi there!

The picture could be above the text in some cases because it can takes time for it to load. This could also causes the img.width and img.height to return 0 (or undefined, don't remember).

After you load the picture, add a listener to it, and then put everything between the let img and return inside that listener function. This will make that the game inly draws the text after the picture is loaded. Therefore, will draw above the picture.

Now I did not understand why are you destroying the window and null it's contents.
It looks like the OP is attempting to modify a function from the TextPicture plugin. The original function from the plugin creates a temporary window in order to utilize Window_Base#textSizeEx, in order to determine what size the bitmap will need to be. It gets rid of the window once it's no longer needed.

However, since the OP's modified function is ignoring the size of the text, and is simply using a fixed width and height, it no longer makes any sense to create/use the window at all.

@Keejeral
As Eliaquim mentioned, ImageManager.loadPicture is an asynchronous function, so when you call it, it will start loading the picture in the background, and will immediately continue to execute the rest of your code, while the picture is loading. This means that when your code gets executed, the width and height of the image haven't been determined yet, so they will probably have a value of 0. It also means that the text would possibly get drawn first, and then get covered up by the image.

And as Mac15001900 mentioned, your arguments for the Bitmap#blt function are wrong.

Additionally, your arguments for the drawTextEx function seem whacky, as you're drawing the text on the far right of the image, and assigning a width of 0 to the text. You're also setting the window (and thus the bitmap) at a flat size of 500x500, and just assuming that both the text and the image will both fit and align correctly. Maybe you intend to use carefully measured text and images, but if not, then you probably need to alter this aspect of your code in some way. It's hard to give a recommendation on what to do about that though, since I can't quite envision what you're going for.
Thank you everyone for your replays! It really helped me out!

I managed to build a stable solution which allows me to put the text above the image.

@Arthran you guessed it right, I used the plugin TextPicture for references, I thought it might fit for the task.
The values of height and width are pretty random and were picked out only for the sake of proof of concept.

The final function looks like that, in case if anyone would need to do something similar:

JavaScript:
 function createTextPicture(text) {
        const tempWindow = new Window_Base(new Rectangle());
        const size = tempWindow.textSizeEx(text);
        tempWindow.move(0, 0, 500,500);
        tempWindow.createContents();   
        const bitmap = ImageManager.loadPicture('1');
        bitmap.addLoadListener(() => {     
            const pw = bitmap.width;
            const ph = bitmap.height;
            tempWindow.contents.blt(bitmap, 0, 0, bitmap.width / 2, bitmap.height /2, 0, 0); 
            tempWindow.drawTextEx(text, 400, 400, 0);
        });
        return tempWindow.contents;
    }

Original thread: https://forums.rpgmakerweb.com/threads/merge-text-and-image-bitmaps.166895/