c# – Image.FromStream()方法返回Invalid Argument异常
发布时间:2020-12-16 01:21:38 所属栏目:百科 来源:网络整理
导读:我从智能相机成像器捕获图像并通过套接字编程从相机接收字节数组(.NET应用程序是客户端,相机是服务器). 问题是我在运行时得到System.InvalidArgument异常. private Image byteArrayToImage(byte[] byteArray) { if(byteArray != null) { MemoryStream ms = n
我从智能相机成像器捕获图像并通过套接字编程从相机接收字节数组(.NET应用程序是客户端,相机是服务器).
问题是我在运行时得到System.InvalidArgument异常. 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中不起作用. 谁能帮我 ? P.S: 我试图完成同样任务的其他方法是: 1. private Image byteArrayToImage(byte[] byteArray) { if(byteArray != null) { MemoryStream ms = new MemoryStream(); ms.Write(byteArray,byteArray.Length); ms.Position = 0; return Image.FromStream(ms,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就必须保持打开状态. 我也很努力地说. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |