I have an application, currently written in C#, which can take a Base64-encoded string and turn it into an Image (a TIFF image in this case), and vice versa. In C# this is actually pretty simple.
private byte[] ImageToByteArray(Image img)
{
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Tiff);
return ms.ToArray();
}
private Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
BinaryWriter bw = new BinaryWriter(ms);
bw.Write(byteArrayIn);
Image returnImage = Image.FromStream(ms, true, false);
return returnImage;
}
// Convert Image into string
byte[] imagebytes = ImageToByteArray(anImage);
string Base64EncodedStringImage = Convert.ToBase64String(imagebytes);
// Convert string into Image
byte[] imagebytes = Convert.FromBase64String(Base64EncodedStringImage);
Image anImage = byteArrayToImage(imagebytes);
(and, now that I'm looking at it, could be simplified even further)
I now have a business need to do this in C++. I'm using GDI+ to draw the graphics (Windows only so far) and I already have code to decode the string in C++ (to another string). What I'm stumbling on, however, is getting the information into an Image object in GDI+.
At this point I figure I need either
a) A way of converting that Base64-decoded string into an IStream to feed to the Image object's FromStream function
b) A way to convert the Base64-encoded string into an IStream to feed to the Image object's FromStream function (so, different code than I'm currently using)
c) Some completely different way I'm not thinking of here.
My C++ skills are very rusty and I'm also spoiled by the managed .NET platform, so if I'm attacking this all wrong I'm open to suggestions.