cwinapirgbbitmapimagebgr

winapi - CreateDIBitmap in 24bpp is in BGR not RGB


I am trying to create an HBITMAP from an array which will contain the color values for the Pixels. The thing is when I try to create a 24-bpp Bitmap, the CreateDIBItmap is using BGR values instead of RGB as I would like.

The code to create the Bitmap is as follows:

 image_size = 600 * 600 * 3;
 aimp_buffer = (char *)malloc(image_size * sizeof(char));
 for (counter = 0; counter < image_size;)
 {
     aimp_buffer[counter++] = 255;
     aimp_buffer[counter++] = 0;
     aimp_buffer[counter++] = 0;
 }

 ads_scrbuf->avo_buffer = (void *)aimp_buffer;
 ads_scrbuf->im_height = 600;
 ads_scrbuf->im_width = 600;
 ads_scrbuf->im_scanline = 600;

 memset(&info, 0, sizeof(info));
 memset(&info.bmiHeader, 0, sizeof(info.bmiHeader));
 info.bmiHeader.biBitCount = 24;
 info.bmiHeader.biHeight= -600;
 info.bmiHeader.biWidth= 600;
 info.bmiHeader.biSize = sizeof(info.bmiHeader);
 info.bmiHeader.biPlanes = 1;
 info.bmiHeader.biCompression = BI_RGB;

 memset(&header, 0, sizeof(BITMAPV5HEADER));
 header.bV5Width = 600;
 header.bV5Height = 600;
 header.bV5BitCount = 24;
 header.bV5Size = sizeof(BITMAPV5HEADER);
 header.bV5Planes = 1;
 header.bV5Compression = BI_RGB;

 *adsp_hBitmap = CreateDIBitmap(GetDC(ds_apiwindow), (BITMAPINFOHEADER *)&header, 
     CBM_INIT, (void *)ads_scrbuf->avo_buffer, &info, DIB_RGB_COLORS)

This should create a Red background for all of the image, but instead it is blue.


Solution

  • The Windows convention for DIB bitmaps is BGR. You can't change that. You will simply have to adapt to it.