Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a class of objects which I store in an xml file, a bitmap list is an object in this class. I'm trying to avoid having to store the bitmap list separately from the other objects. Hence, I convert the bitmap list into bytes (showed in code below) and want to read the byte array from the xml and send it to a list of Bitmaps. First I convert the list of bitmaps into bytes by:
C#
private byte[] BitmapToByteArray(List<Bitmap> bmp)
{
   MemoryStream ms = new MemoryStream();
   foreach(Bitmap item in bmp)
   {
    item.Save(ms, ImageFormat.Bmp);
   }
return ms.ToArray();
}

Now i am trying to create a function which does the reversal but I'm stuck:
C#
private List<Bitmap> ByteToBitmapList(byte[] byteArrayIn)
{
   MemoryStream ms = new MemoryStream(byteArrayIn);
   List<Bitmap> returnList = (List<Bitmap>)Image.FromStream(ms); //this is where I get an error
   return returnList;


How can I return the byte array to the bitmap list? Thanks in advance.

BTW: I'm also storing the bytes in xml because apparently you can't save Images/Bitmaps in xml. So I'm trying to read the byte[] from the xml file and convert it to a list of bitmaps
Posted
Updated 13-Nov-15 7:04am
v3
Comments
BillWoodruff 13-Nov-15 9:34am    
If your bitmaps are in some compressed format, have you considered using WCF to serialize them directly ?
Member 11971544 13-Nov-15 9:39am    
I just updated the question. I'm actually storing the bitmaps(converted to byte[]) in an xml file
BillWoodruff 13-Nov-15 9:44am    
Why would you want to store them in such an "expensive" (memory, computation time) format ? To transmit them "over the wire" ?
Member 11971544 13-Nov-15 9:53am    
Just for clarity, what do you mean by "expensive"? I have a class of objects I store in the xml file, this bitmap list is part of it....trying to avoid having to store the list separately from the others.
BillWoodruff 13-Nov-15 11:52am    
" I have a class of objects I store in the xml file, this bitmap list is part of it....trying to avoid having to store the list separately from the others."

That's important information; I suggest you edit your post to include that information; remember, the more we understand the context you are working in, the clearer our response can be.

I think if there's any way you can store the actual bitmap content "outside" the XML, that will be a win. Storing pointers to files is an idea comes to mind.

Hi,

you saving multiple bitmaps into one MemoryStream. If you want to work this, you need to create byte[][] instead of byte[]. Image.FromStream returns only one image.

C#
private byte[][] BitmapToByteArray(List<Bitmap> bmp)
{
    var res = new List<byte[]>();
    foreach (Bitmap item in bmp)
    {
        using (var ms = new MemoryStream())
        {
            item.Save(ms, ImageFormat.Bmp);
            res.Add(ms.ToArray());
        }
    }
    return res.ToArray();
}


C#
private List<Bitmap> ByteToBitmapList(byte[][] byteArrayIn)
{
    var res = new List<Bitmap>();
    foreach (byte[] img in byteArrayIn)
    {
        using (var ms = new MemoryStream(img))
        {
            var bitmap = (Bitmap)Image.FromStream(ms);
            res.Add(bitmap);
        }
    }
    return res;
}


Hope this helps,
Michal
 
Share this answer
 
v4
Comments
Member 11971544 13-Nov-15 10:10am    
This works like magic, thank you!!!!
jimmson 13-Nov-15 10:13am    
You're welcome :)
BillWoodruff 13-Nov-15 11:57am    
+5 well done !
jimmson 13-Nov-15 12:04pm    
Thank you Bill
Matt T Heffron 13-Nov-15 12:43pm    
+5!
I'll expect you get an InvalidCastException. FromStream returns an Image (just one) and you cast to an List<Bitmap>.

Furtermore you must dispose the memorystream like this:
C#
private Image ByteToBitmap(byte[] byteArrayIn)
{
   using (MemoryStream ms = new MemoryStream(byteArrayIn))
   {
       return Image.FromStream(ms); 
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900