ArgumentException Occurred: Parameter is not valid
<p>I have this code snippet (see below) that I'm working with. I keep getting the above error. Can anyone tell me what I'm doing wrong and how to solve it? Thanks.</p>

<pre><code>private static Image&lt;Bgr, Byte&gt; 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&lt;Bgr, Byte&gt; img = new Image&lt;Bgr, byte&gt;(bmp);

    return img;
}
</code></pre>

<p>I would like to add that, this program works fine from time to time. Some days it doesn't work at all and I don't understand why. I have a presentation and I cannot afford for the program to fail to run on that day.</p>
