Win32 C/C++ Load Image from memory buffer

后端 未结 4 544
日久生厌
日久生厌 2020-12-03 03:47

I want to load an image (.bmp) file on a Win32 application, but I do not want to use the standard LoadBitmap/LoadImage from Windows API: I want it to load from a buffer that

4条回答
  •  我在风中等你
    2020-12-03 04:25

    Nevermind, I found my solution! Here's the initializing 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);
    

提交回复
热议问题