"参数无效"异常负荷为System.Drawing.Image

编程入门 行业动态 更新时间:2024-10-14 06:23:02
本文介绍了"参数无效"异常负荷为System.Drawing.Image的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为什么我得到异常参数无效在我的code:

Why am I getting the exception "Parameter not valid" in my code:

MemoryStream ms = new MemoryStream(byteArrayIn); System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);

byteArrayIn 的长度为169014.我得到这个例外,尽管它没有价值大于255。

The length of byteArrayIn is 169014. I am getting this exception despite the fact that no value in it is greater than 255.

推荐答案

我有同样的问题,显然现在解决了,尽管这和其他一些GDI +的例外是非常误导,我发现,其实问题是,参数为送到一个位图构造器无效。我有这个code:

I had the same problem and apparently is solved now, despite this and some other gdi+ exceptions are very misleading, I found that actually the problem was that the parameter being sent to a Bitmap constructor was not valid. I have this code:

using (System.IO.FileStream fs = new System.IO.FileStream(inputImage, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite)) { try { using (Bitmap bitmap = (Bitmap)Image.FromStream(fs, true, false)) { try { bitmap.Save(OutputImage + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp); GC.Collect(); } catch (Exception ex) { throw ex; } } } catch (ArgumentException aex) { throw new Exception("The file received from the Map Server is not a valid jpeg image", aex); } }

下面一行是造成一个错误:

The following line was causing an error:

Bitmap bitmap = (Bitmap)Image.FromStream(fs, true, false)

的文件流被从地图服务器下载的文件构建的。我的应用程序被错误地发送请求获取图像,服务器返回了与JPG扩展的东西,但实际上是一个HTML告诉我,一个发生错误。所以我采取这一形象,试图建立一个位图用它。此修复程序是控制/验证图像获得有效的JPEG图像。

The file stream was built from the file downloaded from the Map Server. My app was sending the request incorrectly to get the image, and the server was returning something with the jpg extension, but was actually a html telling me that an error ocurred. So I was taking that image and trying to build a Bitmap with it. The fix was to control/ validate the image for a valid jpeg image.

希望它帮助!

更多推荐

"参数无效"异常负荷为System.Drawing.Image

本文发布于:2023-11-12 10:58:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581295.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:负荷   异常   参数   QUOT   Drawing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!