C++ byte array conversion

I have used the following code in C# to load an image column from DataGridView to PictureBox:
var data = (byte[])(dataGridView1.CurrentRow.Cells[3].Value);
var stream = new MemoryStream(data);
PictureBox1.Image = Image.FromStream(stream);
The above code is working perfectly. However I cannot find its implementation in C++. Please help.
a byte in c++ is a char, either signed or unsigned. Typically when storing raw binary file data, you use unsigned bytes.

so that looks like

vector<unsigned char> data(optional_size_of_data_if_known); //you really want to do this for things like images, so get the file size and put it there, possibly with some padding if you want a swap column/row or something for processing.

data[index] = somebyte_from_somewhere; //assign 1 byte into 1 byte, assumes index is allocated (see note on file size).

data.push_back(somebyte); //appends a byte to the container, increasing its size (costly at times)

and if you had opened the image as a binary file, and if you have used the file size as your vector size initialize as above, you can use
yourfile.read(); to load the vector.

I am unsure how to read C# so I am not sure what a datagrid is. If its a microsoftism, you can use it in C++ visual as well. If its from some C# library or built in, we may need to create it. But generally a 1-d vector is all you need for an image in c++.

Dear Jonnin,
Thank you very much for your reply. Actually, yes it is microsoftism. I am loading the data from MySQL database. The image is stored as LONGBLOB type. I am not sure if the above method was good for retrieving images from database, but it did work in Visual C#. I have googled the whole day and was not able to find C++ alternative. Please, be so kind to let me know of any tips you may give on retrieving images from database.
Unfortunately I have no idea, I was hoping that the stream was a file and the container was something simple. The last time I coded for windows, it was called MFC... it would be best if someone not stuck in the 90s helped you with this one...

All I can add here is that a longblob is just a large wad of bytes, so it will fit into a c++ vector as I described for sure. How to get it there... or if you want to use the .net containers/objects, I don't know. Ideally you would have something like executequery("select ...", myvector) but I somehow can't see it being that easy, it never is with microsoft's stuff.

As others mentioned you can load the bitmap into a vector of bytes without problems.
The problem is how do you want to display it?
Do you use some GUI framework already ?
If you don't mind using MFC have a look at this article:
https://www.codeproject.com/Articles/24969/An-MFC-picture-control-to-dynamically-show-picture

Topic archived. No new replies allowed.