Hello,
I need a quick hint on how to properly load e.g. an sf::Texture from memory with the loadFromMemory function. Specifically, I am not sure what precisely the function expects (some data and its size - ok - but what data?). The docs state "load from a file in memory" - does this mean I must construct a .bmp file in memory and pass the pointer to the first byte of the entire file + the size of the file to loadFromMemory?
The error:
Failed to load image from memory. Reason: Image not of any known type, or corrupt
How I obtain my HBITMAP:
OpenClipboard(nullptr);
if (!IsClipboardFormatAvailable(CF_BITMAP))
{
std::cerr << "No Bitmap in clipboard\n";
return -1;
}
HBITMAP bmp = (HBITMAP)GetClipboardData(CF_BITMAP);
CloseClipboard();
Here is my code, as you can see I cluelessly use GetDIBits, but I don't know how to load the sf::Image properly.
bool SFMLLoadHBitmapAsImage(HBITMAP bitmap, sf::Image image)
{
HDC deviceContext = GetDC(nullptr);
if (!deviceContext)
{
return false;
}
//Create BITMAPINFO variable, set size
BITMAPINFO bmpInfo{};
bmpInfo.bmiHeader.biSize = sizeof(bmpInfo.bmiHeader);
//Get the BITMAPINFO structure from the bitmap
// I believe this populates the BITMAPINFOHEADER portion of BITMAPINFO
if (!GetDIBits(deviceContext, bitmap, 0, 0, nullptr, &bmpInfo, DIB_RGB_COLORS))
{
return false;
}
BYTE *pixels = new BYTE[bmpInfo.bmiHeader.biSizeImage];
bmpInfo.bmiHeader.biCompression = BI_RGB;
if (!GetDIBits(deviceContext, bitmap, 0, bmpInfo.bmiHeader.biHeight, (LPVOID)pixels, &bmpInfo, DIB_RGB_COLORS))
{
return false;
}
if (!image->loadFromMemory(?, ?)) // <-- Not sure what goes here
{
return false;
}
delete[] pixels;
ReleaseDC(nullptr, deviceContext);
return true;
}
I pretty much am trying to update the code from here
https://www.codeproject.com/tips/527353/load-an-hbitmap-into-sfml-sf-image-container for SFML2.
This feels like an intuitive solution, but it gives said error:
if (!image->loadFromMemory(pixels, bmpInfo.bmiHeader.biSizeImage))
{
return false;
}
My guess is this is missing something, since the function isn't called loadFromPixels anymore.
I have about 20+ Tabs of msdn open and am actively trying to understand this hot mess of DDBs and DIBs, until then maybe someone shall ease my pain and point me in the right direction. If I find the solution beforehand I'll make sure to update my post.
Kind Regards,
Raincode