【问题标题】:Unhandled System.ArgumentException.. Additional Information: Parameter is not valid未处理的 System.ArgumentException.. 附加信息:参数无效
【发布时间】:2014-03-14 18:36:31
【问题描述】:

我在 c# 中有这段代码,它从数据库中提取图像并在 PictureBox 中显示它们。每当我首先运行代码时,我都会收到此错误消息,提示“System.Drawing.dll 中发生'System.ArgumentException' 类型的未处理异常附加信息:参数无效。”但是如果我终止并重新运行程序,它就可以很好地给出预期的结果。这是给我带来麻烦的部分代码:

 private void buttonGetImage_Click(object sender, EventArgs e)
    {
        string baseUrl = "http://someurl";
        HttpWebRequest request = null;
        foreach (var fileName in fileNames)
        {
            string url = string.Format(baseUrl, fileName);
            MessageBoxButtons buttons = MessageBoxButtons.OKCancel;
            DialogResult result;
            result = MessageBox.Show(url, fileName, buttons);

            if (result == System.Windows.Forms.DialogResult.Cancel)
            {
                this.Close();
            }

            request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = container;

            response = (HttpWebResponse)request.GetResponse();
            Stream stream = response.GetResponseStream();

            byte[] buffer = new byte[10000000];
            int read, total = 0;

            while ((read = stream.Read(buffer, total, 1000)) != 0)
            {
                total += read;
            }
            MemoryStream ms = new MemoryStream(buffer, 0, total);
            ms.Seek(0, SeekOrigin.Current);
            Bitmap bmp = (Bitmap)Bitmap.FromStream(ms);
            pictureBoxTabTwo.Image = bmp;
            this.pictureBoxTabTwo.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBoxTabTwo.Image.Save("FormTwo.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        }
    }

有人可以帮我弄清楚可以做什么吗? 错误显示我行 --> Bitmap bmp = (Bitmap)Bitmap.FromStream(ms);

【问题讨论】:

  • 在传递给 MemoryStream (ms) 之前测试您的总数是否为零。看看这是否能让你更接近解决方案。
  • 这段代码在您的应用程序中的什么位置?
  • @RadioSpace 不,它不是在抛出异常时......在其他情况下......无论如何我可以解决这个问题?
  • 我只是认为有时您正在创建一个零大小的内存流并试图使用它。你能排除这个吗?
  • 喜欢使用 if (ms.length == 0) ?

标签: c# argumentexception


【解决方案1】:

我在我的程序中使用了 Image 类,而不是使用 Bitmap 类。我在这里所做的是获取一个流并将其放入一个字节数组中。并再次将该数组的内容转换回流。相反,我使用了

Image img = Image.FromStream(stream)

在这种情况下,您甚至不必使用 MemoryStream。它现在非常适合我。

【讨论】:

    【解决方案2】:

    很难从您的代码中判断问题可能是什么,但在某些情况下,您在服务器上进行的查询可能会返回意外响应。当出现问题时,您最好获取返回流的快照。它将使您能够诊断问题并采取适当的措施。

    【讨论】:

      【解决方案3】:

      您需要进行适当的对象处理。否则,在垃圾收集器赶上对象之前,底层连接不会关闭,这可能会导致问题。此外,在 .NET 4.0 及更高版本中,您可以在 Streams 上使用 CopyTo 方法。

              request = (HttpWebRequest)WebRequest.Create(url);
              request.Method = "GET";
              request.ContentType = "application/x-www-form-urlencoded";
              request.CookieContainer = container;
      
              using (response = (HttpWebResponse)request.GetResponse())
              using (Stream stream = response.GetResponseStream())
              {
                  // All of this code is unnecessary if using .NET 4.0 or higher.
                  /*
                  byte[] buffer = new byte[10000000];
                  int read, total = 0;
      
                  while ((read = stream.Read(buffer, total, 1000)) != 0)
                  {
                      total += read;
                  }
                  MemoryStream ms = new MemoryStream(buffer, 0, total);
                  ms.Seek(0, SeekOrigin.Current);
                  */
                  // Instead use the following
                  MemoryStream ms = new MemoryStream();
                  stream.CopyTo(ms);
      
                  Bitmap bmp = (Bitmap)Bitmap.FromStream(ms);
                  pictureBoxTabTwo.Image = bmp;
                  this.pictureBoxTabTwo.SizeMode = PictureBoxSizeMode.Zoom;
                  pictureBoxTabTwo.Image.Save("FormTwo.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
              }
      

      【讨论】:

      • 正确处置将是第二次运行而不是第一次运行的问题,对吧?
      • 据说,在这一行抛出异常“ Bitmap bmp = (Bitmap)Bitmap.FromStream(ms);”
      • 在上面的代码中,“ms”和“stream”是如何交互的?如果没有,如何将响应字符串传递给代码?
      【解决方案4】:

      你可以试试

      pictureBox1.Image = new Bitmap(sourceBitmap);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-13
        • 1970-01-01
        • 2017-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-12
        相关资源
        最近更新 更多