Drawing Image Using Bitmap

I’m playing around with displaying images from an SD card, which is working well, but is very slow. On another thread, it said drawing an image from a bitmap class is much faster, but there wasn’t much elaboration on how to do this. Is there anyone who can teach me how to display images form bitmap?

You can read the images into memory once at the beginning of the program and then use the drawImageFromBuffer function.

/**
 * @brief Draws an image on the screen using the contents of the memory buffer.
 * @param buffer A pointer to a buffer containing image data in either bmp or png format.
 * @param x The x-coordinate at which the left edge of the image will be drawn.
 * @param y The y-coordinate at which the top edge of the image will be drawn.
 * @param bufferLen The size of the source image buffer in bytes.
 * @return Returns true if the image was successfully drawn on the screen.
 * @details
 *  This function draws an image on the screen using the contents of a buffer into which
 *  either BMP or PNG raw data has already been read.  The contents may have come from a
 *  file on the SD card or have been statically declared in the code.  The image should be
 *  no larger than the V5 Screen, that is, a maximum of 480 pixels wide by 272 pixels high.
 *  The top/left corner of the image is placed at the coordinates given by x and y, these can
 *  be negative if desired.
*/
bool     drawImageFromBuffer( uint8_t  *buffer, int x, int y, int bufferLen );

or, if the images are small, embed them directly into the code, see this example.

3 Likes

How exactly did you initially insert an image into the brain’s memory?

You read it from the SD card into memory once

Is that through a command or file transferring somehow?

To use the drawImageFromBuffer method you would need to read it into memory with normal C/C++ file I/O methods, the sdcard’s loadFile method, or write its data directly into your program (like the example in James’ linked post does). I don’t use these three methods often for image files, so your mileage may vary.

If you’re using Robot Mesh Studio to write your C++, there’s a shortcut through the use of a bitmap object:

// At the start of the function (only called once):
bitmap my_img("example_filename.png");

// later when you want to draw it:
Brain.Screen.drawBitmap(my_img, 0, 0);
1 Like

I’m using VexCODE, so what would be the simplest method to use and how would I use it?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.