如何克服参数是无效的异常 [英] How to overcome Parameter is Invalid Exception
问题描述
我在检索图像时遇到参数无效异常(错误仅发生在Win7上但在XP OS中正常工作)
这是我的代码
I am getting a parameter invalid exception while retrieving an image (error only occurs on Win7 but working fine in XP OS)
Here is my code
Image _image = null;
ImageConverter imgConverter = null;
_image = imgConverter.ConvertFrom(pItem.FrontImage) as System.Drawing.Image; //pItem.FrontImage is Byte[]
我试图使用
I have tried to use
MemoryStream ms = new MemoryStream(pItem.FrontImage);
ms.Write(pItem.FrontImage, 0, pItem.FrontImage.Length);
_image = Image.FromStream((Stream)ms);
但没有用。
你能不能请帮助我?
完成错误:
but no use.
Can you please help me?
Complete error:
System.ArgumentException: Parameter is not valid.
at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
at System.Drawing.Image.FromStream(Stream stream)
有没有其他方法可以从 Byte []转换
不使用 image
FromStream
。
我试图用 File.WriteAllBytes <保存图像/ code>但没有用(图片显示为已损坏)。
is there any other way to convert image
from Byte[]
without using FromStream
.
I have tried to save the Image with File.WriteAllBytes
but no use (Image is showing as corrupted).
推荐答案
试试这个:
// requires using System.IO
using (MemoryStream ms = new MemoryStream(pItem.FrontImage))
{
_image = Image.FromStream(ms);
}
如果这不起作用,那么在代码中放一个断点:Image _image = null;然后使用F11单步执行。请注意发生错误的位置,并在原始问题中发布错误的位置。
If this does not work, then put a break-point in your code on the line: Image _image = null; and then single-step using F11. Note where any error occurs, and post where the error occurred here in your original question.
当stream为null或stream不包含有效的图像数据时,会收到此消息。 />
这行代码是不必要的,因为你直接通过构造函数转发了图像数据:
You get this message when stream is null or when stream doesn't contain valid image data.
This line of code is unnecessary as you have forwarded image data directly via constructor:
ms.Write(pItem.FrontImage, 0, pItem.FrontImage.Length);
所以试试这个:
so try this:
MemoryStream ms = new MemoryStream(pItem.FrontImage);
_image = Image.FromStream(ms,true,true);
我已经尝试了你提到的所有解决方案。对我来说没有用。我们因为Multi Page Tiff Image而得到.Same Image可以正常使用Windows Xp所以我们不能认为这不是一个有效的图像.Byte数组也不是空值.FromStream不工作。请让我知道任何其他解决方案。我已经尝试过你提到的方法和手段。
I have tried with all the solutions you have mentioned .All are not useful for me .We are getting because of Multi Page Tiff Image .Same Image can be working fine with Windows Xp so we cannt think this is not a valid image .Byte array is not null value too.FromStream is not working .Please let me know any other solution .I have already tried the ways and means you have mentioned .
这篇关于如何克服参数是无效的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!