“参数无效” 加载 System.Drawing.Image 时出现异常
为什么我在代码中收到“参数无效”异常:
MemoryStream ms = new MemoryStream(byteArrayIn);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
byteArrayIn
的长度为 169014。尽管事实上其中没有值大于 255,但我还是收到此异常。
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);
The length of byteArrayIn
is 169014. I am getting this exception despite the fact that no value in it is greater than 255.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我遇到了同样的问题,显然现在已经解决了,尽管这个问题和其他一些 gdi+ 异常非常具有误导性,但我发现实际上问题是发送到位图构造函数的参数无效。 我有以下代码:
以下行导致错误:
文件流是根据从地图服务器下载的文件构建的。 我的应用程序错误地发送了获取图像的请求,服务器返回了带有 jpg 扩展名的内容,但实际上是一个 html 告诉我发生了错误。 所以我拍摄了该图像并尝试用它构建位图。
解决方法是控制/验证图像以获得有效的 jpeg 图像。
希望能帮助到你!
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:
The following line was causing an error:
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.
Hope it helps!
我的猜测是
byteArrayIn
不包含有效的图像数据。请提供更多信息:
byteArrayIn
?您确定它应该包含有效的图像吗?My guess is that
byteArrayIn
doesn't contain valid image data.Please give more information though:
byteArrayIn
from, and are you sure it should contain a valid image?Image.FromStream()
抛出的“参数无效”异常告诉您该流不是“有效”或“可识别”格式。 观察内存流,特别是当您从文件中获取各种字节偏移量时。示例 2 将起作用,请注意 useEmbeddedColorManagement 必须为 false 才能使 validateImageData 有效。
通过将内存流转储到文件并检查内容可能是最简单的调试方法。
The "parameter is not valid" exception thrown by
Image.FromStream()
tells you that the stream is not a 'valid' or 'recognised' format. Watch the memory streams, especially if you are taking various offsets of bytes from a file.Example 2 will work, note that useEmbeddedColorManagement must be false for validateImageData to be valid.
May be easiest to debug by dumping the memory stream to a file and inspecting the content.
哪一行抛出异常?
new MemoryStream(...)
? 或者Image.FromStream(...)
?byteArrayIn
是什么? 它是一个字节[]吗? 我只是因为注释“并且其中的任何值都不大于 255”而问 - 这当然对于byte[]
来说是自动的。一个更明显的问题是:二进制文件实际上包含合理格式的图像吗?
例如,以下代码(虽然不是很好的代码)可以正常工作:
Which line is throwing the exception? The
new MemoryStream(...)
? or theImage.FromStream(...)
? And what is thebyteArrayIn
? Is it abyte[]
? I only ask because of the comment "And none of value in it is not greater than 255" - which of course is automatic for abyte[]
.As a more obvious question: does the binary actually contain an image in a sensible format?
For example, the following (although not great code) works fine:
此错误是由于将二进制数据插入缓冲区而引起的。
要解决此问题,您应该在代码中插入一条语句。
该语句是:
示例:
This error is caused by binary data being inserted into a buffer.
To solve this problem, you should insert one statement in your code.
This statement is:
Example:
Image.FromStream() 方法来自 .NET 文档
我的意思是,主要问题是字节数组的格式不正确。
您可以使用 validateImageData 参数,但它仍然会抛出异常。
我注意到,如果您的字节数组是从
png
、jpg
等旧扩展创建的,则Image.FormStream()
方法只能正确进行流式传输。 我使用带有webp
的图像,它抛出异常。 您必须转换为旧格式(例如 jpeg)才能正确使用。 有几个用于转换图像的库。我建议 SixLabors.ImageSharp.Web
首先
安装库后
Image.FromStream() method from .NET Documents
I mean, the main problem is that the byte array is not in proper format.
You can use
validateImageData
parameter but it will still throw exception..I noticed that the
Image.FormStream()
method is only streaming properly if your byte array created frompng
,jpg
etc. old extensions. I used an image withwebp
an it throws exception. You have to convert to old formats like jpeg to use properly. there are several libraries for convert images.I advice SixLabors.ImageSharp.Web
First
After install the library
给出的所有解决方案都不起作用..不要只专注于检索部分。 很高兴插入图像。 我也犯了同样的错误。 我从硬盘中取出图像并将其保存到数据库中。 问题出在插入命令上。 卢克在我的错误代码..:
上面的代码显示成功插入...但实际上它以错误的数据类型的形式保存图像..而数据类型必须bt“图像”..所以我改进了代码..100
%保证检索时不会出现 PARAMETER NOT VALID 错误....已解决!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!
all the solutions given doesnt work.. dont concentrate only on the retrieving part. luk at the inserting of the image. i did the same mistake. I tuk an image from hard disk and saved it to database. The problem lies in the insert command. luk at my fault code..:
The above code shows succesfully inserted... but actualy its saving the image in the form of wrong datatype.. whereas the datatype must bt "image".. so i improved the code..
100% gurantee that there will be no PARAMETER NOT VALID error in retrieving....SOLVED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
大多数情况下,发生这种情况是 SQL 列中的数据错误。 这是插入图像列的正确方法:
大多数人都错误地这样做:
Most of the time when this happens it is bad data in the SQL column. This is the proper way to insert into an image column:
Most people do it incorrectly this way:
只需按照此操作将值插入数据库
//连接字符串
Just Follow this to Insert values into database
//Connection String