转换列表<位图>字节数组并存储在xml c#中 [英] Convert List<Bitmap> to byte array and store in xml c#

查看:62
本文介绍了转换列表<位图>字节数组并存储在xml c#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一类对象存储在xml文件中,位图列表是此类中的对象。我试图避免必须将位图列表与其他对象分开存储。因此,我将位图列表转换为字节(在下面的代码中显示),并希望从xml读取字节数组并将其发送到位图列表。首先,我将位图列表转换为字节:

  private   byte  [] BitmapToByteArray(List< Bitmap> bmp)
{
MemoryStream ms = new MemoryStream();
foreach (位图项 in bmp)
{
item。保存(ms,ImageFormat.Bmp);
}
return ms.ToArray();
}



现在我正在尝试创建一个能够实现逆转的功能但是我被卡住了:

 私人列表<位图> ByteToBitmapList( byte  [] byteArrayIn)
{
MemoryStream ms = new MemoryStream( byteArrayIn);
列表<位图> returnList =(List< Bitmap>)Image.FromStream(ms); // 这是我收到错误的地方
return returnList;





如何将字节数组返回到位图列表?在此先感谢。



BTW:我也将字节存储在xml中,因为显然你无法在xml中保存图像/位图。所以我试图从xml文件中读取byte []并将其转换为位图列表

解决方案





您将多个位图保存到一个MemoryStream中。如果你想这样做,你需要创建byte [] []而不是byte []。 Image.FromStream只返回一个图像。



  private   byte  [] [] BitmapToByteArray(List< Bitmap> bmp)
{
var res = new 列表< byte [] > ();
foreach (位图项 in bmp)
{
使用 var ms = new MemoryStream())
{
item.Save(ms,ImageFormat.Bmp);
res.Add(ms.ToArray());
}
}
return res.ToArray();
}





  private 列表<位图> ByteToBitmapList( byte  [] [] byteArrayIn)
{
var res = < span class =code-keyword> new List< Bitmap>();
foreach byte [] img in byteArrayIn)
{
使用 var ms = new MemoryStream(img))
{
var 位图=(位图)Image.FromStream(女士);
res.Add(bitmap);
}
}
return res;
}





希望这会有所帮助,

Michal


< blockquote>我希望你得到一个InvalidCastException。 FromStream返回一个Image(只有一个),然后你转换为List< Bitmap> ;.



另外你必须像这样处理内存流:

  private  Image ByteToBitmap( byte  [] byteArrayIn)
{
使用(MemoryStream ms = new MemoryStream(byteArrayIn))
{
return Image.FromStream(ms);
}
}


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:

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:

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

解决方案

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.

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();
}



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


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:

private Image ByteToBitmap(byte[] byteArrayIn)
{
   using (MemoryStream ms = new MemoryStream(byteArrayIn))
   {
       return Image.FromStream(ms); 
   }
}


这篇关于转换列表&lt;位图&gt;字节数组并存储在xml c#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆