问题
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