views:

238

answers:

2

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  A: 

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.

Eric Perko
You are right. This should work. However, you may want to compress the data.
George Edison
Some MySQL BLOB examples say to encode the data so it is in printable range to avoid any *dangerous* binary values. Also, how does one create an image from the data?
Thomas Matthews
+1  A: 

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.

VZ
Last time I tried saving uncompressed, I could not create the image from the data. When I went the temporary file route, there were no problems with the image. The API says not to use `SetData` unless you know what you are doing.
Thomas Matthews
Even although I do *not* advise doing it like this, there is still really no reason whatsoever this shouldn't work so all I can say is that there must have been some bug in your code somewhere. Unfortunately it's impossible to be more precise without any additional information.
VZ