windows Win32 C/C++ 从内存缓冲区加载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2886831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Win32 C/C++ Load Image from memory buffer
提问by Bruno
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 is already in memory. I can easily load a bitmap directly from a file and print it on the screen, but this issue is making me stuck.
我想在 Win32 应用程序上加载图像 (.bmp) 文件,但我不想使用 Windows API 中的标准 LoadBitmap/LoadImage:我希望它从内存中已经存在的缓冲区加载。我可以轻松地直接从文件加载位图并将其打印在屏幕上,但是这个问题让我卡住了。
What I'm looking for is a function that works like this:
我正在寻找的是一个像这样工作的函数:
HBITMAP LoadBitmapFromBuffer(char* buffer, int width, int height);
采纳答案by Bruno
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);
回答by Adam Rosenfield
Try CreateBitmap()
:
HBITMAP LoadBitmapFromBuffer(char *buffer, int width, int height)
{
return CreateBitmap(width, height, 1, 24, buffer);
}
回答by JustJeff
CreateDIBSection
can be a little complicated to use, but one of the things it can do is create a device-independent bitmap and give you a pointer to the buffer for the bitmap bits. Granted, you already have a buffer full of bitmap bits, but at least you could copy the data.
CreateDIBSection
使用起来可能有点复杂,但它可以做的一件事是创建一个与设备无关的位图,并为您提供一个指向位图位缓冲区的指针。当然,您已经有一个充满位图位的缓冲区,但至少您可以复制数据。
Speculating a bit: CreateDIBSection
can also create bitmaps from file objects, and there's probably a way to get Windows to give you a file object representing a chunk of memory, which might trick CreateDIBSection
into giving you a bitmap built directly from your buffer.
推测一下:CreateDIBSection
也可以从文件对象创建位图,并且可能有一种方法让 Windows 为您提供一个表示内存块的文件对象,这可能会欺骗CreateDIBSection
您直接从您的缓冲区构建位图。
回答by Billy ONeal
No, but you can create a new bitmap the size of the current one in memory, and write your memory structure onto it.
不,但您可以在内存中创建一个与当前位图大小相同的新位图,然后将您的内存结构写入其中。
You're looking for the CreateBitmapfunction. Set lpvBits to your data.
您正在寻找CreateBitmap函数。将 lpvBits 设置为您的数据。