【问题标题】:ArgumentException Occurred: Parameter is not valid发生 ArgumentException:参数无效
【发布时间】:2011-09-08 14:53:43
【问题描述】:

我正在使用此代码 sn-p(见下文)。我不断收到上述错误。谁能告诉我我做错了什么以及如何解决?谢谢。

private static Image<Bgr, Byte> GetImageFromIPCam(string sourceURL)
{
    byte[] buffer = new byte[300000];
    int read, total = 0;

    // create HTTP request
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sourceURL);

    // get response
    WebResponse resp = req.GetResponse();

    // get response stream
    Stream stream = resp.GetResponseStream();

    // read data from stream
    while ((read = stream.Read(buffer, total, 1000)) != 0)
    {
        total += read;
    }

    // get bitmap
    Bitmap bmp = (Bitmap)Bitmap.FromStream( //error occurs here
        new MemoryStream(buffer, 0, total)); //error occurs here

    Image<Bgr, Byte> img = new Image<Bgr, byte>(bmp);

    return img;
}

我想补充一点,这个程序有时运行良好。有些日子它根本不起作用,我不明白为什么。我有一个演示文稿,但我不能让程序在那天无法运行。

【问题讨论】:

  • 您有实际的异常文本吗?应该提供一个行号以便更快地参考。编辑:错过了 cmets... nm
  • 告诉我们抛出错误的那一行
  • 您是否确认缓冲区不为空(并且包含有效的图像格式)?

标签: c# argumentexception


【解决方案1】:

根据MSDN构造函数

public MemoryStream(byte[] buffer, int index, int count)

当 index 和 count 的总和大于缓冲区的长度时,抛出 ArgumentException。验证 total 变量是否包含小于 buffer 的正确值。

【讨论】:

  • 也会试试这个。我会让你知道它是否有效。谢谢
  • 目前,“total”不输出任何内容。所以我正在遍历 while 循环,看看我是否遗漏了什么。
【解决方案2】:

参数异常

您的情况下的偏移量“0”和您的情况下的计数“total”的总和大于缓冲区长度。

this

试试

byte [] buffer= new byte[total]; 

在while循环之后做这个语句

【讨论】:

  • 你不是说在while循环之前吗?当我输入“byte[] buffer = new byte[total];”在 while 循环之后,我收到错误消息“在声明之前不能使用局部变量“缓冲区”,这是准确的。但是当我将它放在 while 循环之前时,我收到此消息:ArgumentOutOfRange Exception:Specified argument is out有效值的范围。参数名称:此代码行的大小:while ((read = stream.Read(buffer, total, 1000)) != 0)
【解决方案3】:

人们试图获取 IP 摄像机的当前图像时经常会看到此错误。原因是许多 IP 摄像机在 URL 上呈现自己的网页,而您将网页视为图像,这将永远无法正常工作。

大多数 IP 摄像机都有一个可以提供当前图像的 URL,您应该改用它。如果你不知道它是什么,这里是一个起点:

http://www.bluecherrydvr.com/2012/01/technical-information-list-of-mjpeg-and-rtsp-paths-for-network-cameras/

【讨论】:

    猜你喜欢
    • 2012-05-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    相关资源
    最近更新 更多