如何使用C++将wxImage存储到数据库中?
我有一些 wxImages,我想将它们存储到 BLOB(二进制大型对象)字段。
wxImage 和 wxBitmap 用于获取作为 unsigned char
数组的二进制数据,以便我可以加载到数据库。
我当前的解决方法是将图像写入临时文件,然后直接从该文件加载 BLOB 字段。
是否有更有效的方法来加载 wxImage 对象并将其存储到 MySQL BLOB 字段中?
我正在使用 MySql C++ 连接器 1.05、MS Visual Studio 2008、wxWidgets 和 C++。
I have some wxImages and I would like to store them into a BLOB (Binary Large OBject) field in a MySQL database.
There are no methods in wxImage nor wxBitmap for obtaining the binary data as an array of unsigned char
so I can load into the database.
My current workaround is to write the image to a temporary file, then load the BLOB field directly from the file.
Is there a more efficient method to load and store a wxImage object into a MySQL BLOB field?
I am using MySql C++ connector 1.05, MS Visual Studio 2008, wxWidgets and C++.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
wxWidgets 不为来自
wxBitmap
的数据提供任何 API(因为它依赖于平台),但wxImage
使用定义明确(且非常简单)的格式,您可以访问该格式使用上面提到的GetData()
方法。请注意,如果您的图像具有 Alpha 通道,您可能还需要使用GetAlpha()
。然而,这不是我这样做的方式,因为如果你这样做的话,数据将会巨大。虽然如上所述,压缩它是可能的,但当 wxImage 已经支持以任何标准图像格式写入图像时,为什么还要手动进行压缩呢?只需创建一个
wxMemoryOutputStream
并将其传递给SaveFile()
即可。然后只需使用 GetOutputStreamBuffer()->GetBufferStart() 及相关函数直接访问流缓冲区即可。wxWidgets doesn't provide any API to the data from
wxBitmap
(because it's platform-dependent) butwxImage
uses a well-defined (and very simple) format which you can access using itsGetData()
method as mentioned above. Just notice that you may need to useGetAlpha()
as well if your images have alpha channel.However this is not how I'd do it because the data will be huge if you do it like this. While compressing it, as also suggested above, is possible, why bother doing it manually when
wxImage
already supports writing image in any of the standard image formats. Just create awxMemoryOutputStream
and pass it toSaveFile()
. Then simply access the stream buffer directly usingGetOutputStreamBuffer()->GetBufferStart()
and related functions.除非我遗漏了一些东西,否则你不能使用 WxImage::GetData (http:// /docs.wxwidgets.org/2.8/wx_wximage.html#wximagegetdata)来获取数据,然后::GetHeight和::GetWidth来知道指针指向的数据的长度? WxImage::GetData 返回的 unsigned char * 看起来应该指向组成图像的 RGB 数据。
Unless I'm missing something, couldn't you use WxImage::GetData (http://docs.wxwidgets.org/2.8/wx_wximage.html#wximagegetdata) to get the data and then ::GetHeight and ::GetWidth to know the length of the data that pointer points to? The unsigned char * that is returned by WxImage::GetData looks like it should point to the RGB data that makes up the image.