Thread: Unhandled exception on Stream variable for HttpWebRequest/HttpWebResponse

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058

    Unhandled exception on Stream variable for HttpWebRequest/HttpWebResponse

    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);
            }
        }
    }

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Edit: Forget what I just said. Can you provide a stack trace?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    According to the documentation, that exception is thrown when the stream doesn't contain a valid image format. Have you tried saving that stream down to a local file and opening it that way?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Yep, that's what it is. The JPEG stream I captured is bogus. I couldn't open it in a paint program. There is definitely something wrong with the format.

    Thanx all!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GetSaveFileName exception
    By stanlvw in forum Windows Programming
    Replies: 4
    Last Post: 07-04-2008, 11:42 AM
  2. Debugger: Unhandled Exception: User Breakpoint
    By JaWiB in forum Windows Programming
    Replies: 1
    Last Post: 12-18-2005, 05:46 PM
  3. I got an unhandled exception!!
    By LegendBreath in forum Game Programming
    Replies: 7
    Last Post: 04-19-2005, 01:55 PM
  4. Linked List: Unhandled Exception
    By wbeasl in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2004, 02:03 PM
  5. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 03:02 PM