Uncaught exception.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Airslash
    New Member
    • Nov 2007
    • 221

    #1

    Uncaught exception.

    Hello,

    here is my code piece. I know it's ugly written, but the idea is that I recieve an image over TCP IP and try to display it in a picturebox.

    Code:
                            try
                            {
                                WriteMessage("Image Data recieved.");
                                int size = m_client.Available;
                                WriteMessage("Image Size: " + size.ToString() + " bytes.");
                                byte[] buffer = new byte[size];
                                nstream.Read(buffer, 0, size);
    
                                MemoryStream mstream = new MemoryStream();
                                mstream.Write(buffer, 0, size);
    
                                Image img = Image.FromStream(mstream);
    
                                m_display.Image = img;
    
                                img.Dispose();
                                mstream.Close();
    
                                // Ask next image.
                                WriteMessage("Asking next image.");
                                byte[] request = Encoding.ASCII.GetBytes(txt_message.Text);
    
                                if (nstream.CanWrite)
                                {
                                    nstream.Write(request, 0, request.Length);
                                }
                                else
                                {
                                    WriteMessage("Can't write next request.");
                                }
                            }
                            catch (Exception Ex)
                            {
                                WriteMessage(Ex.Message);
                            }
    I'm getting an ArgumentExcepti on thrown here and I can't seem to catch , although the code is surrounded with try-catch blocks.
    The argument exception complains about an invalid parameter is beeing related to image width. At least thats all I can tell from the exception.

    The function itself it beeing called at a regular interval using a Threading.Timer class.

    EDIT:
    did some debugging, the image get's transfered correctly. Saved it to disk and it displays correctly. So no idea where the problem is comming from.
  • Airslash
    New Member
    • Nov 2007
    • 221

    #2
    Here's the details of the exception

    Code:
    System.ArgumentException was unhandled
      Message=Parameter is not valid.
      Source=System.Drawing
      StackTrace:
           at System.Drawing.Image.get_Width()
           at System.Drawing.Image.get_Size()
           at System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
           at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
           at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
           at System.Windows.Forms.Control.WmPaint(Message& m)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at VTTest.Program.Main() in E:\Source Code\CurrentRelease\Visual Studio\UnitTests\VTTest\VTTest\Program.cs:line 18
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException:
    So if I understand this correctly, the problem is related with drawing the image on the display?

    Comment

    • Aimee Bailey
      Recognized Expert New Member
      • Apr 2010
      • 197

      #3
      You are disposing the image before m_display has time to render it, let the garbage collector do it for you and you may find it works. Also, make sure to catch more specific exceptions, using the base Exception class means that you get only a general handler for the exception.

      Best of luck :)

      Aimee.

      Comment

      • Airslash
        New Member
        • Nov 2007
        • 221

        #4
        Why is the solution always this bloody simple.....


        I have removed the dispose and it works....Still getting an invalid parameter exception now and then but the images get displayed at least.

        I've noticed that I have a serious "lag" on the display and alot of the images get corrupt on the display. Is this a transfer issue between server & client, or am I simply overwriting them too fast?

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          Oh this exception.. ugh, yea I've encounters this too.

          The details are fuzzy, but I think when you dispose that object it destroys something the memory stream is holding onto.

          Comment

          • Aimee Bailey
            Recognized Expert New Member
            • Apr 2010
            • 197

            #6
            Something else I just noticed, should you be using the Image class, or its base class Bitmap?

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #7
              Oops, nevermind my last post. I misread and thought you were doing something else.

              That said, I don't think you can close the memory stream. When I was fooling around with loading images and I closed the memory stream, I would get that exception... I think. I'm going to have to dig through my example project.

              Comment

              • Airslash
                New Member
                • Nov 2007
                • 221

                #8
                I'll give it a go without closing or disposing stuff. Might move them to class members and simply reuse them.

                Currently I'm using the Image base class because I'm loading jpeg from a TCP stream.Whether or not these remain jpeg I don't know yet.
                I'm simply trying to emulate a videostream, because I don't want to use a third-party component to render avi streams.

                In theory this should work just as well :S

                Comment

                • Aimee Bailey
                  Recognized Expert New Member
                  • Apr 2010
                  • 197

                  #9
                  One question, are you loading them one by one? or are you buffering a couple at a time before playing?

                  Stupid question I know, but have to ask :$

                  Comment

                  • Airslash
                    New Member
                    • Nov 2007
                    • 221

                    #10
                    I'm loading them one by one.

                    The idea is that the client requests an Image, this image gets loaded & transfered. Then the client displays the image and sends the next request.

                    So it's practicly the bandwith + client processing speed that determine the speed of the transfer.

                    I have set the NoDelay option to true on my TcpListener/Client to make sure that the transfers don't have to wait for a full buffer.

                    Comment

                    • Airslash
                      New Member
                      • Nov 2007
                      • 221

                      #11
                      Update:

                      Setting the NoDelay on the client to true, but false on the server seems to produce the fastest results at the moment.

                      Comment

                      • Airslash
                        New Member
                        • Nov 2007
                        • 221

                        #12
                        UPDATE:

                        Well after playing around with various settings and making a lot of configurable, I've found some conclusions:

                        - The client polls the line every 10ms
                        - The server polls the line every 75ms
                        - The client sends a request and receives image data.
                        - The client needs around 16ms to send a request, get a reply and render the whole image.
                        - The server needs 100ms to complete a request, because the UDP response from the third applications takes so long.

                        Is it correct to conclude that the TCP connection works as intended, but that the following issues occur:

                        - Server gets slowed down due the UDP taking so long
                        - THe client 'lags' because I overwrite the image faster then its beeing displayed

                        Comment

                        Working...