【问题标题】:Parameter is not valid. Stack Trace at System.Drawing.Bitmap..ctor(Stream stream)参数无效。 System.Drawing.Bitmap..ctor 的堆栈跟踪(流流)
【发布时间】:2016-05-11 12:31:06
【问题描述】:

在某些情况下,只有我收到此错误。 “参数在 System.Drawing.Bitmap..ctor(Stream stream) 处无效堆栈跟踪”我有点困惑它是如何为某些记录工作的,为什么不为其他记录工作。任何人请指导我找到我的错误将非常有帮助..,

以下是我的代码。,

private void RefreshImage()
    {
        if (this.dsPatPhoto.dmDoc.Count <= 0) return;

        byte[] patImage = null;
        byte[] driverLicImage = null;

        foreach (CmData.WrCmDoc.DsCmDoc.dmDocRow row in this.dsPatPhoto.dmDoc)
        {
            if (!row.IsIDDocTypeNull() &&
                row.IDDocType == (short)AppCommonCfg.DocType.PatientDriverLicense)
            {
                if (!row.IsDocImageNull())
                    driverLicImage = row.DocImage;
            }
            else
            {
                if (!row.IsDocImageNull())
                    patImage = row.DocImage;
            }
        }

        System.IO.MemoryStream stream;
        if (patImage != null && patImage.Length > 0)
        {
            stream = new System.IO.MemoryStream(patImage, true);
            this.ucPictureEditPic.Clear();
            this.ucPictureEditPic.Image = new System.Drawing.Bitmap(stream);
        }

        if (driverLicImage != null && driverLicImage.Length > 0)
        {
            stream = new System.IO.MemoryStream(driverLicImage, true);
            this.ucPictureEditDL.Clear();
            this.ucPictureEditDL.Image = new System.Drawing.Bitmap(stream); //Error occurs here.
        }

    }

【问题讨论】:

  • 存储在数据集中的图像是有效图像吗?如果构造函数无法弄清楚字节数据的结构,它将无法从字节构造图像。如果您从已知文件加载字节并将其与数据集中的字节进行比较,它们是否匹配?另请参阅Bitmap reference source
  • @theB-感谢您的回复-但我不确定我的图片是否无效。如果它是一个损坏的图像意味着我能做什么..,有没有办法将它转换成有效的图像。通过代码我们可以使它成为可能吗??
  • 从参考源看来,位图类使用 GDI+ 创建图像。有 7 个 GDI 错误会导致构造函数抛出 ArgumentException 1) 无效参数,2) 未知图像格式,3) 未找到属性,4) 不支持属性,5-7) 各种字体问题。鉴于问题的描述,我愿意消除除#2之外的所有内容。如果将数据集中的数据保存到文件中,是否可以在图像编辑器中打开该文件? (IE - 遇到异常时使用System.IO.File.WriteAllBytes() 将字节数组转储到文件中。)
  • @theB-sorry for ask you can post a sample code将非常有帮助..

标签: c# bitmap stream bytearray memorystream


【解决方案1】:

使用Reference Source 我们可以看到位图类正在使用GDI+ 本地方法来构建图像。从参考源中,我们还可以看到构造函数可能抛出的list of exceptions。在所有可以抛出的异常中,ArgumentException 可能来自 8 个地方。

  1. 流为空。
  2. 参数无效。
  3. 未知的图像格式。
  4. 找不到属性。
  5. 不支持属性。
  6. 未找到字体系列。
  7. 未找到字体样式。
  8. 不是 True Type 字体。

我们可以立即消除 #6-8,因为您没有尝试渲染字体。我们也可以消除#1,因为流对象是在调用位图构造函数的正上方创建的。数字 2、4 和 5 的评估稍微复杂一些,但我已经排除了它们的可能性,因为内存流对于构建位图是有效的。 (我经常使用它作为渲染基于 Web 的图像的首选方法。)

这给我们留下了未知的图像格式。有两种方法可以检查字节数组是否有效。

  1. 从文件中加载图像的副本并将字节与DataSet中的字节进行比较。

    if (driverLicImage != null && driverLicImage.Length > 0)
    {
        byte[] knownGoodImage = System.IO.File.ReadAllBytes("Path to good file on disk");
        if (!driverLicImage.SequenceEqual(knownGoodImage))
        {
            // now you know that the bytes in the database don't match
        }
        stream = new System.IO.MemoryStream(driverLicImage, true);
        this.ucPictureEditDL.Clear();
        this.ucPictureEditDL.Image = new System.Drawing.Bitmap(stream); //Error occurs here.
    }
    
  2. 捕获构造函数异常并将文件保存到磁盘,以便您可以尝试使用图像编辑器打开它。 (如 MS Paint)

    if (driverLicImage != null && driverLicImage.Length > 0)
    {
        try
        {
            stream = new System.IO.MemoryStream(driverLicImage, true);
            this.ucPictureEditDL.Clear();
            this.ucPictureEditDL.Image = new System.Drawing.Bitmap(stream); //Error occurs here.
        }
        catch (ArgumentException ex)
        {
            System.Diagnostics.Debug.Print(ex.Message);
            System.IO.File.WriteAllBytes("Filename", driverLicImage);
        }
    }
    

    当然,您需要选择一个合适的文件名。

【讨论】:

  • @theB-非常感谢。你救了我很多。实际上,该字节值不是图像类型,而是 pdf 文件。但是我在将字节写入txt时才知道这一点。现在我解决了我的错误。你的帮助真的很感激
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 1970-01-01
  • 1970-01-01
  • 2013-05-20
相关资源
最近更新 更多