【发布时间】: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