Image.FromStream()方法返回Invalid Argumentexception

我正在从智能相机成像器捕获图像并通过套接字编程从相机接收字节数组(.NET应用程序是客户端,相机是服务器)。

问题是我在运行时得到System.InvalidArgumentexception。

private Image byteArrayToImage(byte[] byteArray) { if(byteArray != null) { MemoryStream ms = new MemoryStream(byteArray); return Image.FromStream(ms, false, false); /*last argument is supposed to turn Image data validation off*/ } return null; } 

我在许多论坛上搜索过这个问题,并尝试了许多专家给出的建议,但没有任何帮助。

我不认为字节数组有任何问题,因为当我将相同的字节数组输入我的VC ++ MFC客户端应用程序时,我得到了图像。 但这在C#.NET中不起作用。

谁能帮我 ?

PS:

我试图完成同样任务的其他方法是:

1。

 private Image byteArrayToImage(byte[] byteArray) { if(byteArray != null) { MemoryStream ms = new MemoryStream(); ms.Write(byteArray, 0, byteArray.Length); ms.Position = 0; return Image.FromStream(ms, false, false); } return null; } 

2。

 private Image byteArrayToImage(byte[] byteArray) { if(byteArray != null) { TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap)); Bitmap b = (Bitmap)tc.ConvertFrom(byteArray); return b; } return null; } 

上述方法均无效。 请帮助。

Image.FromStream()需要一个只包含一个图像的流!

它将stream.Position重置为0.我有一个包含多个图像或其他内容的流,你必须将你的图像数据读入一个字节数组并初始化一个MemoryStream

Image.FromStream(new MemoryStream(myImageByteArray));

只要图像正在使用, MemoryStream就必须保持打开状态。

我也很努力地说。 🙂

也许图像嵌入在OLE字段中,您必须考虑88字节的OLE标头加上有效负载:

 byteBlobData = (Byte[]) reader.GetValue(0); stream = new MemoryStream(byteBlobData, 88, byteBlobData.Length - 88); img = Image.FromStream(stream); 

我猜测从服务器接收文件时出现了问题。 也许你只是在尝试将其转换为Image之前才获得文件的一部分? 你确定它是你为C ++应用程序提供的完全相同的字节数组吗?

尝试将流保存到文件中,看看你得到了什么。 你或许可以在那里发现一些线索。

您还可以添加断点并手动将字节数组中的某些字节与它们应该的字节进行比较(如果您知道的话)。


编辑:看起来接收数据没有任何问题。 问题是它是原始格式(不是Image.FromStream理解的格式)。 Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr)构造函数可能在这里使用。 或者,您可以创建空白位图并从原始数据手动blt。

这样做时我遇到了这个问题:

 MemoryStream stream = new MemoryStream(); screenshot.Save(stream, ImageFormat.Png); byte[] bytes = new byte[stream.Length]; stream.Save(bytes, 0, steam.Length); 

最后两行是问题所在。 我通过这样做修复它:

 MemoryStream stream = new MemoryStream(); screenshot.Save(stream, ImageFormat.Png); byte[] bytes = stream.ToArray(); 

然后这工作:

 MemoryStream stream = new MemoryStream(bytes); var newImage = System.Drawing.Image.FromStream(stream); stream.Dispose(); 

System.InvalidArgument表示流没有有效的图像格式,即不支持的图像类型。

试试这个:

 public Image byteArrayToImage(byte[] item) { Image img=Image.FromStream(new MemoryStream(item)); img.Save(Response.OutputStream, ImageFormat.Gif); return img; } 

希望能帮助到你!

我在过去遇到了同样的问题,它是由Windows GDI库中的泄漏引起的,这就是“Bitmap”所使用的。 如果这种情况一直发生在你身上,那么它可能是无关紧要的。

这段代码正在运行

  string query="SELECT * from gym_member where Registration_No ='" + textBox9.Text + "'"; command = new SqlCommand(query,con); ad = new SqlDataAdapter(command); DataTable dt = new DataTable(); ad.Fill(dt); textBox1.Text = dt.Rows[0][1].ToString(); textBox2.Text = dt.Rows[0][2].ToString(); byte[] img = (byte[])dt.Rows[0][18]; MemoryStream ms = new MemoryStream(img); pictureBox1.Image = Image.FromStream(ms); ms.Dispose(); 

尝试使用类似于此处描述的内容https://social.msdn.microsoft.com/Forums/vstudio/en-US/de9ee1c9-16d3-4422-a99f-e863041e4c1d/reading-raw-rgba-data-into-一个位图

 Image ImageFromRawBgraArray( byte[] arr, int charWidth, int charHeight, int widthInChars, PixelFormat pixelFormat) { var output = new Bitmap(width, height, pixelFormat); var rect = new Rectangle(0, 0, width, height); var bmpData = output.LockBits(rect, ImageLockMode.ReadWrite, output.PixelFormat); // Row-by-row copy var arrRowLength = width * Image.GetPixelFormatSize(output.PixelFormat) / 8; var ptr = bmpData.Scan0; for (var i = 0; i < height; i++) { Marshal.Copy(arr, i * arrRowLength, ptr, arrRowLength); ptr += bmpData.Stride; } output.UnlockBits(bmpData); return output; }