5 years, 2 months ago.

how can I load an image to screen?

how can I load an image to screen?

Question relating to:

The STM32F746G-DISCO discovery board (32F746GDISCOVERY) is a complete demonstration and development platform for STMicroelectronics ARM® Cortex®-M7 core-based STM32F746NGH6 microcontroller.

5 Answers

4 years, 6 months ago.

Hello, all,

I am using this code Staron Paul. The code can contain an image correctly, but when I load 2 or more images, the device becomes stuck and not load the next image (only the first image that can be loaded).

is there any advice for me?

thank you

Try removing /2 from the memory allocation and see if it helps. I'm running the code loading images over and over again without any problems. Should be: buffer = (char*) malloc (sizeof(char)*fileSize); Seems to me that it's only allocating half the file size, hence writes outside allocated memory. (if I'm not mistaken).

posted by Olle Sköld 05 Feb 2018
3 years, 5 months ago.

Hi, first of all thanks a lot Paul Staron for posting this code, it really helped me a lot to get forward with my project!

However, I found one thing that I do not really understand. To me it looks like you are allocating half the memory size of fileSize? (fileSize/2) as sizeof(char) returns 1. Why is that?

If there aren't anything obvious that I've missed, wouldn't this mean that when you write to buffer, half of the file will be written to un-allocated memory which could explain why your system freezes.

5 years, 2 months ago.

Check line 924, BSP_LCD_DrawBitmap


You could change this to read from SD or filesystem, but for much faster draw speed it is better to load the image to internal RAM in one hit rather than line by line as most other examples use.

For an example, Line 488, Bitmap16RAM does this for 16 bit BMP images on the ssd1331 driver chip.



This may help, replace the BSP DrawBitmap function with this:


void BSP_LCD_DrawBitmap(uint32_t Xpos, uint32_t Ypos, uint8_t *Name_BMP)
  #include <stdlib.h>
  uint32_t index = 0, width = 0, height = 0, bit_pixel = 0;
  uint32_t address;
  uint32_t input_color_mode = 0;
  char filename[50];
  int i, fileSize;
  char * buffer;
    while (*Name_BMP!='\0') {
  filename[i] = 0; 
    FILE *Image = fopen((const char *)&filename[0], "rb");  // open the bmp file
   // obtain file size:
    fseek (Image , 0 , SEEK_END);
    fileSize = ftell (Image);
    rewind (Image);
    // allocate memory to contain the whole file:
    buffer = (char*) malloc (sizeof(char)*fileSize/2);
    // copy the file into the buffer:
    fseek (Image, 0 , SEEK_SET );  // set SD file data start position    
    fread (buffer,1,fileSize,Image);
    fclose (Image);
  /* Get bitmap data address offset */
  index = *(__IO uint16_t *) (buffer + 10);
  index |= (*(__IO uint16_t *) (buffer + 12)) << 16;
  /* Read bitmap width */
  width = *(uint16_t *) (buffer + 18);
  width |= (*(uint16_t *) (buffer + 20)) << 16;
  /* Read bitmap height */
  height = *(uint16_t *) (buffer + 22);
  height |= (*(uint16_t *) (buffer + 24)) << 16; 
  /* Read bit/pixel */
  bit_pixel = *(uint16_t *) (buffer + 28);   
  /* Set the address */
  address = hLtdcHandler.LayerCfg[ActiveLayer].FBStartAdress + (((BSP_LCD_GetXSize()*Ypos) + Xpos)*(4));
  /* Get the layer pixel format */    
  if ((bit_pixel/8) == 4)
    input_color_mode = CM_ARGB8888;
  else if ((bit_pixel/8) == 2)
    input_color_mode = CM_RGB565;   
    input_color_mode = CM_RGB888;
  /* Bypass the bitmap header */
  buffer += (index + (width * (height - 1) * (bit_pixel/8)));  
  /* Convert picture to ARGB8888 pixel format */
  for(index=0; index < height; index++)
    /* Pixel format conversion */
    LL_ConvertLineToARGB8888((uint32_t *)buffer, (uint32_t *)address, width, input_color_mode);

    /* Increment the source and destination buffers */
    address+=  (BSP_LCD_GetXSize()*4);
    buffer -= width*(bit_pixel/8);
  free (buffer); 

Import this 'SD FileSystem'


The code below works, but will only display one image, locks if trying to display a subsequent image and is loaded into almost all the available RAM, very fast display write, I did try a sequential line by line SD transfer file but could not get that working. Best option would be to transfer the image to the on board SDRAM rather than MCU RAM.

sd image

#include "mbed.h"
#include "SDFileSystem.h"
#include "LCD_DISCO_F746NG.h"

DigitalOut myled(LED1);
SDFileSystem sd("sd");

int main() { 
    lcd.DrawBitmap(0,0,(uint8_t *)"/sd/minion480x272.bmp");        
    while(1) {
        myled = 1; 
        myled = 0; 

Here is the 16bit BMP image that will fit in the screen, edited using GIMP. Save it to the root of your SD card.


Hi, i add sd (and it's read the card) to the program, but how can I don't know how to change the program to open the image.

posted by Pedro Cruz 28 May 2016

Doesn't work for me... Any special sdcard format? also i cannot see the image in my pc (Macbook air). Can you please try it again? Thank you very much for you effort.

posted by thethings.iO Platform 21 Oct 2016

Not using Mac so can't say why. I right hand click the link and 'save target as'. Works on my PC (Windows 7).

posted by Paul Staron 21 Oct 2016

this one is good one.. can you help me too sir, for implementing machine learning for nRF51822

posted by rakha asyrofi 17 Apr 2018
3 years, 3 months ago.


I tried to pull together code that I thought would do what you suggested (Paul Staron) but I was unsuccessful. I am not sure if some of the libraries have changed since this was originally posted.

Do you a completed project or code that you can share that has been published?

Thanks. Josh

I haven't checked, I think these libraries have been changed since the updated Mbed OS.

posted by Paul Staron 17 Apr 2018

If anyone else reads this and has the same issue, I published the following demo which can read bitmap files from SD-card and display them: https://os.mbed.com/users/Lightsource/code/DISCO-F746NG_BMP_FROM_SDCARD/ It is based on the project linked here.

posted by Olle Sköld 24 Apr 2018
5 years, 1 month ago.

Hello, can I ask you for a demo program how to load an image from SD card with SMT32F7NG? Just to understand and try by myself after. Thanks so much, Marco

hi, I tried this program https://developer.mbed.org/users/MikamiUitOpen/code/F746_SpectralAnalysis_Example/

and worked, but I can not load my images, probably I made a mistake in my image conversion to array

posted by Pedro Cruz 11 Jun 2016

Hello Pedro, .... I solved my problem but not in elegant way. Instead to use drawbitmap I used drawpixel. I saw programs by 不韋 呂 very good programmer. Code in attach: /media/uploads/billycorgan123/code.txt

Hoping it helps you. As soon as possible i will post a code for example. Bye, Marco

posted by marco valli 12 Jun 2016

Check my edited answer...

posted by Paul Staron 17 Jun 2016