"参数无效"异常负荷为System.Drawing.Image [英] "Parameter not valid" exception loading System.Drawing.Image

查看:1351
本文介绍了"参数无效"异常负荷为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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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