c# - "Parameter not valid"加载System.Drawing.Image异常

标签 c# image stream argumentexception

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

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/

相关文章:

jQuery - 获取图像的宽度

android - 将多个图像合并为一个图像

java - Jasper 和 .xlsx 的内容消息不可读

c# - 如何在 LINQ to Entities 中重用字段过滤器

c# - 何时使用 BlockingCollection 以及何时使用 ConcurrentBag 而不是 List<T>?

html - 当我选择一个图像时,如何防止图像列表被重置?

c++ - boost::asio::streambuf 为空?

c# - 编辑 List<Tuple> 项

c# - 多个 WebClient 不工作?

c# - 多路复用流? (同时读/写)