【问题标题】:System Argument exception parameter is not valid while trying to display a bitmap尝试显示位图时系统参数异常参数无效
【发布时间】:2016-06-05 13:22:06
【问题描述】:

一直在尝试各种选项来在图片框中显示矩形人脸捕获,已经尝试了几天,但感觉我已经接近此代码了

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

【问题讨论】:

  • 你能发布你的序列化代码吗?
  • 位图构造函数将抛出ArgumentException,如果你传递的流不包含已知的图像格式,并且你没有序列化图像,你只是序列化一个对象输入Car。如果没有要加载的图像,如何加载图像?
  • @theB 是一名 PHP 程序员,我正在尝试 C#,理解并尊重您的评论,只是觉得这个很难让我理解
  • 道歉jackofnotrades,如果这被认为是敌对的,我正在尝试(并且显然失败)提出一个可能导致您得到答案的问题。 @jdweng 下面的回答更清楚地指出了我想要达到的目的。
  • @theB 表示尊重您的评论,从您的评论中认为我可能发现了我的错误,jdweng 然后确认了它,感谢您的意见。

标签: c# image serialization memorystream


【解决方案1】:

我发现有两点不对。 1) Car Class 中没有图像,只有一个名称。然后 Bitmap.FromStream 正在使用整个 Car 类来获取您需要忽略名称并仅使用图像的图像。

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

【讨论】:

  • 在阅读您的帖子并正确查看后,我假设从我的代码中我试图加载一个对象而不是图像,谢谢 m8
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多