开发者

IP camera streaming error

开发者 https://www.devze.com 2023-03-26 16:20 出处:网络
I have a Samsung IP camera and I want to stream it in to my c# program, but when I run the program I got\'invalid parameter\' error.

I have a Samsung IP camera and I want to stream it in to my c# program, but when I run the program I got 'invalid parameter' error.

 private void button1_Click(object sender, EventArgs e) { while (true) {     

        string  sourceURL = url; byte[] buffer = new byte[100000];
        int read, total = 0;
        // create HTTP request
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sourceURL);
        req.Credentials = new NetworkCredential("admin", "开发者_运维问答4321");
        // 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;
        }

        Bitmap bmp = (Bitmap)Bitmap.FromStream(new MemoryStream(buffer, 0, total));
        pictureBox1.Image = bmp;
    }
}

What might be the problem?


You are not building the correct buffer, you are overriding the old buffer with new buffer each time while there is new data, idea to fix it:

List<byte> fullData = new List<Byte>();

while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)//also > 0 not == 0 because of it can be -1
{
    fullData.AddRange(new List<Byte>(buffer).GetRange(0, read));//only add the actual data read
}

byte[] dataRead = fullData.ToArray();

Bitmap bmp = (Bitmap)Bitmap.FromStream(new MemoryStream(dataRead , 0, dataRead.Lenght));


My guess (since you don't indicate the error) is that the image has gone over 100,000 bytes, which your code doesn't handle at all. I would, instead:

byte[] buffer = new byte[10 * 1024];
...
using(var ms = new MemoryStream())
{
    // read everything in the stream into ms
    while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
    {
        ms.Write(buffer, 0, read);
    }
    // rewind and load the bitmap
    ms.Position = 0;
    Bitmap bmp = (Bitmap)Bitmap.FromStream(ms);
    pictureBox1.Image = bmp;
}


kinda late answer relative to the time of the question; however, since I'm hunting down similar issues and this has apparently not been answered, I'm adding my two cents...

There are two issues here as I see it:

First:

You don't wanna put the event handler of a button-click into an endless loop. This should probably be a thread of some type so that the event handler can return.

Second:

As mentioned in another comment, your code is expecting the response to be a raw image of some type, and most likely it is not. Your camera may eventually send MJPG, but that doesn't mean it comes raw. Sometimes you have to send other commands to the camera and then when you actually start getting the MJPG stream you have to parse it and extract headers prior to sending the portion of the image to some picturebox. You're probably getting some kind of html response from the camera (as I am) and when you try to pass that data to a method that is expecting the data to be some image format (likely JPEG), then you get the invalid parameter error.

Can't say I know how to solve the problem cause it depends on the camera. If there is some kind of standard interface for these cameras I'd sure like to know what it is! Anyway, HTH...

0

精彩评论

暂无评论...
验证码 换一张
取 消