Search code examples
c#imageserializationmemorystream

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


Have been trying various options to display a rectangular face capture in a picture box, have been trying for a few days but feel I got close with this code

//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;
 }

The parameter not valid exception is just above in the

Var imageToSave = Bitmap.FromStream(Stream);

Can anyone give me any advice as where to go next?

Serializer code

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

Solution

  • I found two things wrong. 1) There was no image in the Car Class only a name. Then Bitmap.FromStream was using the entire Car class to get image where you need to ignore name and just use the image.

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