尝试显示位图时系统参数异常参数无效

System Argument exception parameter is not valid while trying to display a bitmap

一直在尝试各种选项来在图片框中显示矩形面部捕捉,已经尝试了几天,但感觉我已经接近这个代码了

//preparing FaceRecord
fr.FacePosition = new FSDK.TFacePosition();
fr.FacialFeatures = new FSDK.TPoint[FSDK.FSDK_FACIAL_FEATURE_COUNT];
fr.Template = new byte[FSDK.TemplateSize];
fr.image = new FSDK.CImage();

fr.image = fr.image.CopyRect((int)(fr.FacePosition.xc - Math.Round(fr.FacePosition.w * 0.5)), (int)(fr.FacePosition.yc - Math.Round(fr.FacePosition.w * 0.5)), (int)(fr.FacePosition.xc + Math.Round(fr.FacePosition.w * 0.5)), (int)(fr.FacePosition.yc + Math.Round(fr.FacePosition.w * 0.5)));

Car car = new Car();
car.Name = "Temp";                   

MemoryStream stream = Serializer.SerializeToStream(car);
System.IO.File.WriteAllBytes("Temp.Jpeg", stream.ToArray());

using (var Stream = new MemoryStream(System.IO.File.ReadAllBytes("Temp.Jpeg")))
{
    Car cab = (Car)Serializer.DeserializeFromStream(Stream);
    var imageToSave = Bitmap.FromStream(Stream);

    pictureBox2.Height = imageToSave.Height;
    pictureBox2.Width = imageToSave.Width;
    pictureBox2.Image = imageToSave;
 }

参数无效异常就在

上面
Var imageToSave = Bitmap.FromStream(Stream);

任何人都可以给我任何关于下一步去哪里的建议吗?

序列化程序代码

public class Serializer
    {

        public static MemoryStream SerializeToStream(object o)
        {
            MemoryStream stream = new MemoryStream();
            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, o);
            return stream;
        }

        public static object DeserializeFromStream(MemoryStream stream)
        {
            IFormatter formatter = new BinaryFormatter();
            stream.Seek(0, SeekOrigin.Begin);
            object o = formatter.Deserialize(stream);
            return o;
        }
    }

[Serializable]
    public class Car
    {
        public string Name;                       
    }

我发现有两点不对。 1) Car Class 中没有图像,只有名字。然后 Bitmap.FromStream 使用整个汽车 class 来获取图像,而您需要忽略名称而只使用图像。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {

            Car car = new Car();
            car.image = File.ReadAllBytes(@"c:\temp\image1.jpg");
            car.Name = "Temp";
            MemoryStream stream = Serializer.SerializeToStream(car);
            System.IO.File.WriteAllBytes("Temp.Jpeg", stream.ToArray());

            using (var Stream = new MemoryStream(System.IO.File.ReadAllBytes("Temp.Jpeg")))
            {
                Car cab = (Car)Serializer.DeserializeFromStream(Stream);
                var imageToSave = Bitmap.FromStream(new MemoryStream( cab.image));

            }
        }
    }
    public class Serializer
    {

        public static MemoryStream SerializeToStream(object o)
        {
            MemoryStream stream = new MemoryStream();
            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, o);
            return stream;
        }

        public static object DeserializeFromStream(MemoryStream stream)
        {
            IFormatter formatter = new BinaryFormatter();
            stream.Seek(0, SeekOrigin.Begin);
            object o = formatter.Deserialize(stream);
            return o;
        }
    }

    [Serializable]
    public class Car
    {
        public string Name { get; set; }
        public byte[] image { get; set; }
    }
}