“参数无效"异常加载 System.Drawing.Image [英] "Parameter not valid" exception loading System.Drawing.Image

查看:60
本文介绍了“参数无效"异常加载 System.Drawing.Image的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的代码中出现异常参数无效":

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+ 异常非常具有误导性,但我发现实际上问题在于参数被发送到位图构造函数无效.我有这个代码:

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);
    }
}

以下行导致错误:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆