How to store wxImage into database, using C++?

核能气质少年 提交于 2019-12-08 13:45:00

问题


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++.


回答1:


wxWidgets doesn't provide any API to the data from wxBitmap (because it's platform-dependent) but wxImage uses a well-defined (and very simple) format which you can access using its GetData() method as mentioned above. Just notice that you may need to use GetAlpha() 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 a wxMemoryOutputStream and pass it to SaveFile(). Then simply access the stream buffer directly using GetOutputStreamBuffer()->GetBufferStart() and related functions.




回答2:


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.



来源:https://stackoverflow.com/questions/2548130/how-to-store-wximage-into-database-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!