从内存加载图像,GDI+
这是一个快速简单的问题:使用 C++ 中的 GDI+,如何从内存中的像素数据加载图像?
Here's a quick and easy question: using GDI+ from C++, how would I load an image from pixel data in memory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用SHCreateMemStream,它需要一个指向数据的指针和数据的大小。
Use SHCreateMemStream, it takes a pointer to the data and the size of the data.
可能不像您希望的那么容易,但您可以使用像素数据在内存中创建 BMP 文件:
如有必要,请将像素数据转换为 BITMAP 友好格式。如果您已经拥有 24 位 RGB 像素数据,则可能不需要转换。
创建(在内存中)一个 BITMAPFILEHEADER 结构,后跟一个 BITMAPINFO 结构。
现在您已经获得了所需的内容,您需要将其放入 IStream 中,以便 GDI+ 可以理解它。也许最简单(尽管不是最高效)的方法是:
现在,调用 GDI+ Image::FromStream() 方法将图像加载到 GDI+ 中。
祝你好运!
Probably not as easy as you were hoping, but you can make a BMP file in-memory with your pixel data:
If necessary, translate your pixel data into BITMAP-friendly format. If you already have, say, 24-bit RGB pixel data, it is likely that no translation is needed.
Create (in memory) a BITMAPFILEHEADER structure, followed by a BITMAPINFO structure.
Now you've got the stuff you need, you need to put it into an IStream so GDI+ can understand it. Probably the easiest (though not most performant) way to do this is to:
Now, call the GDI+ Image::FromStream() method to load your image into GDI+.
Good luck!
有一个位图构造函数,它直接接受 BITMAPINFO 和指向像素数据的指针,例如:
这对于 RGB 图像来说没问题,如果它是调色板图像,则需要为 RGBQUADS 等分配足够空间的 BITMAPINFO 。
There's a bitmap constructor which takes a BITMAPINFO and a pointer to pixel data directly, for example:
That's OK for an RGB image, if it's a palette image you'd need to allocate a BITMAPINFO with enough space for the RGBQUADS etc etc.