Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 奇怪的二进制格式化程序行为_.net_Binaryformatter - Fatal编程技术网

.net 奇怪的二进制格式化程序行为

.net 奇怪的二进制格式化程序行为,.net,binaryformatter,.net,Binaryformatter,我有一个带有图像数组的结构: public struct ObjectImages { public System.Drawing.Image[] _images; public ObjectImages(System.Drawing.Image[] images) { _images = images; } } 如果我在对象中放置两个10KB的图像,然后尝试序列化,我发现我的内存流目标有160kb。我验证过,每个图像都有10-11kb

我有一个带有图像数组的结构:

public struct ObjectImages
{

    public System.Drawing.Image[] _images;
    public ObjectImages(System.Drawing.Image[] images)
    {
          _images = images; 
    }

}
如果我在对象中放置两个10KB的图像,然后尝试序列化,我发现我的内存流目标有160kb。我验证过,每个图像都有10-11kb

ObjectImages o = new ObjectImages(x); // where x is an array of images from my webcam
MemoryStream ms = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, o); 

我觉得很奇怪!你能告诉我问题出在哪里吗

好吧,当你在硬盘上看到图像时,它会被压缩,例如PNG或JPEG文件。在C#中加载图像时,图像将被解压缩,因此可以渲染像素。所以在现实中,图像包含的字节比存储在硬盘上的字节还要多


C#对象包含未压缩的数据,因此大致上(对于RGBA图像),内存中的大小应该是
(4*width*height)+更多.NET所需的数据。二进制格式化程序不保存图像(PNG或JPEG),而是保存表示图像的对象。

10~11 kb磁盘上…使用哪种格式?此外,它将序列化原始图像数据和反序列化对象所需的信息。谢谢!因此,我应该将图像以字节[]的形式保存,并将MyImage.save(myMemoryStream,System.Drawing.Imaging.ImageFormat.Jpeg)结果保存在我的结构中;