Win32 C / C ++ Load Image from Memory Buffer

I want to load an image file (.bmp) into a Win32 application, but I do not want to use the standard LoadBitmap / LoadImage from the Windows API: I want it to be loaded from a buffer that is already in memory. I can easily download the bitmap directly from the file and print it on the screen, but this problem makes me stuck.

I am looking for a function that works as follows:

HBITMAP LoadBitmapFromBuffer(char* buffer, int width, int height);
+5
source share
4 answers

Nevermind, I found my solution! Here's the initialization code:

std::ifstream is;
is.open("Image.bmp", std::ios::binary);
is.seekg (0, std::ios::end);
length = is.tellg();
is.seekg (0, std::ios::beg);
pBuffer = new char [length];
is.read (pBuffer,length);
is.close();

tagBITMAPFILEHEADER bfh = *(tagBITMAPFILEHEADER*)pBuffer;
tagBITMAPINFOHEADER bih = *(tagBITMAPINFOHEADER*)(pBuffer+sizeof(tagBITMAPFILEHEADER));
RGBQUAD             rgb = *(RGBQUAD*)(pBuffer+sizeof(tagBITMAPFILEHEADER)+sizeof(tagBITMAPINFOHEADER));

BITMAPINFO bi;
bi.bmiColors[0] = rgb;
bi.bmiHeader = bih;

char* pPixels = (pBuffer+bfh.bfOffBits);

char* ppvBits;

hBitmap = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, (void**) &ppvBits, NULL, 0);
SetDIBits(NULL, hBitmap, 0, bih.biHeight, pPixels, &bi, DIB_RGB_COLORS);

GetObject(hBitmap, sizeof(BITMAP), &cBitmap);
+5
source

Try CreateBitmap():

HBITMAP LoadBitmapFromBuffer(char *buffer, int width, int height)
{
    return CreateBitmap(width, height, 1, 24, buffer);
}
+3
source

CreateDIBSection , , , -. , , , .

: CreateDIBSection , , , Windows -, , CreateDIBSection, .

+3

No, but you can create a new raster image of the size of the current in memory and write your memory structure on it.

You are looking for the CreateBitmap function . Install lpvBits in your data.

0
source

All Articles