I'm getting an unhandled exception at the statement in red in the following code. Being somewhat new to C#, I'm not sure on how to resolve it. The debug output is as follows:
Code:
$exception {"Parameter is not valid."} System.Exception {System.ArgumentException}
this {CCTVStream.VideoStream, Text: CCTV Video Streaming} CCTVStream.VideoStream
sURL "http://192.168.1.102/cgi-bin/nph-zms?mode=jpeg&monitor=1&user=guest&pass=guest" string
buffer {Dimensions:[100000]} byte[]
str {System.Net.ConnectStream} System.IO.Stream {System.Net.ConnectStream}
[System.Net.ConnectStream] {System.Net.ConnectStream} System.Net.ConnectStream
base {System.Net.ConnectStream} System.IO.Stream {System.Net.ConnectStream}
CanRead true bool
CanSeek false bool
CanTimeout true bool
CanWrite false bool
Length '((System.Net.ConnectStream)(str)).Length' threw an exception of type 'System.NotSupportedException' long {System.NotSupportedException}
Position '((System.Net.ConnectStream)(str)).Position' threw an exception of type 'System.NotSupportedException' long {System.NotSupportedException}
ReadTimeout 300000 int
WriteTimeout 300000 int
Static members
Non-Public members
base {System.Net.ConnectStream} System.MarshalByRefObject {System.Net.ConnectStream}
CanRead true bool
CanSeek false bool
CanTimeout true bool
CanWrite false bool
Length 'str.Length' threw an exception of type 'System.NotSupportedException' long {System.NotSupportedException}
Position 'str.Position' threw an exception of type 'System.NotSupportedException' long {System.NotSupportedException}
ReadTimeout 300000 int
WriteTimeout 300000 int
Static members
Non-Public members
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.IO;
namespace CCTVStream
{
/// <summary>
/// Summary description for PictureBoxDemo.
/// </summary>
public class VideoStream : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private Image GetImage(string sURL)
{
byte[] buffer = new byte[100000];
Stream str = null;
try
{
//Create a web request to the url containing the image
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(sURL);
//optional: use only if you're using a proxy to connect to the internet
//wReq.Proxy = SetWebProxy();
//gets the response from the web request
HttpWebResponse wRes = (HttpWebResponse)(wReq).GetResponse();
//return the image stream from the URL specified earlier
str = wRes.GetResponseStream();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
if (str != null)
{
MessageBox.Show("returning bitmap");
return Image.FromStream(str);
}
else
{
return null;
}
}
public VideoStream()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.Text = "CCTV Video Streaming";
this.button1.Text = "Display";
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pictureBox1.Location = new System.Drawing.Point(8, 8);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(360, 200);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// button1
//
// this.button1.Location = new System.Drawing.Point(232, 24);
this.button1.Location = new System.Drawing.Point(160, 220);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(56, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
// PictureBoxDemo
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(376, 254);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.pictureBox1}
);
this.Name = "VideoStreamDemo";
this.Text = "VideoStreamDemo";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new VideoStream());
}
private void button1_Click(object sender, System.EventArgs e)
{
CaptureVideo();
}
private void CaptureVideo()
{
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
Image image;
image = GetImage("http://192.168.1.102/cgi-bin/nph-zms?mode=jpeg&monitor=1&user=guest&pass=guest");
pictureBox1.Image = new System.Drawing.Bitmap(image);
}
}
}