未处理的System.ArgumentException ..附加信息:参数无效

时间:2014-03-14 18:36:31

标签: c# argumentexception

我在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);
        }
    }

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

4 个答案:

答案 0 :(得分:1)

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

Image img = Image.FromStream(stream)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您需要进行适当的物品处理。否则,在垃圾收集器赶上对象之前,底层连接不会关闭,这可能会导致问题。此外,在.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);
        }

答案 3 :(得分:0)

你可以尝试一下

pictureBox1.Image = new Bitmap(sourceBitmap);