参数无效。 System.Drawing.Bitmap..ctor的堆栈跟踪(流媒体流)

时间:2016-05-11 12:31:07

标签: c# bitmap stream bytearray memorystream

在某些情况下,我才会收到此错误消息。 "参数无效在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.
        }

    }

1 个答案:

答案 0 :(得分:1)

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

  1. Stream为空。
  2. 参数无效。
  3. 未知图像格式。
  4. 未找到财产。
  5. 不支持的属性。
  6. 找不到字体系列。
  7. 找不到字体样式。
  8. 非True Type字体。
  9. 我们可以立即消除#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);
          }
      }
      

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