为什么我的代码中出现“参数无效”异常:
MemoryStream ms = new MemoryStream(byteArrayIn);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
byteArrayIn
的长度是 169014。尽管其中没有任何值大于 255,但我得到了这个异常。
最佳答案
我遇到了同样的问题,现在显然已经解决了,尽管这个和其他一些 gdi+ 异常非常具有误导性,但我发现实际上问题在于发送到 Bitmap 构造函数的参数无效。我有这段代码:
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);
}
}
以下行导致错误:
Bitmap bitmap = (Bitmap)Image.FromStream(fs, true, false)
文件流是根据从 map 服务器下载的文件构建的。我的应用程序错误地发送了获取图像的请求,服务器返回了带有 jpg 扩展名的内容,但实际上是一个 html 告诉我发生了错误。所以我正在拍摄该图像并尝试用它构建位图。 解决方法是控制/验证有效 jpeg 图像的图像。
希望对您有所帮助!
关于c# - "Parameter not valid"加载System.Drawing.Image异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/629955/