Пример #1
0
        //Method for displaying the image for the video stream
        private void DisplayImage(IPCamera iPCamera, byte[] payload)
        {
            Image image = null;

            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    //Write data of the payload into memory
                    ms.Write(payload, 0, payload.Length);
                    //LOOK LOAD IMAGE HERE
                    image            = Image.FromStream(ms);
                    cameraLabel.Text = camera.CameraName;
                    //PictureBox myPictureBox = new PictureBox();
                    myPictureBox.SizeMode   = PictureBoxSizeMode.StretchImage;
                    myPictureBox.ClientSize = new Size(camera.Width, camera.Height);
                    myPictureBox.Image      = image;
                }
            } catch (Exception ex)
            {
                MessageBox.Show("Error", "\n" + ex.Message);
            }
        }
Пример #2
0
        //https://stackoverflow.com/questions/1080442/how-to-convert-an-stream-into-a-byte-in-c
        //Method for reading data from the server
        private void ReadData()
        {
            String decodedData = null; //storing the decoded response from the server

            byte[]   tempJPEGArray = null;
            string[] ipArr         = null; //temp array
            int      sizeOfPayload = 0;
            string   bufferString  = "";



            try
            {
                //Create stream
                NetworkStream stream = client.GetStream();
                //check if client is connected
                if (IsConnected(client))
                {
                    int buffer = 0; //buffer to temp keep track for every byte read.
                    //While loop to ensure every byte is read until the end of the stream.
                    while ((buffer = stream.ReadByte()) != -1)
                    {
                        char tempChar = (char)buffer;
                        //bufferString += tempChar;
                        switch (tempChar)
                        {
                        case '[':
                            continue;     //skip character

                        case ']':
                            //Get the array for the temp IP header and the size of the jpeg
                            ipArr         = bufferString.Replace(']', ' ').Split('|');
                            sizeOfPayload = Convert.ToInt32(ipArr[6]);
                            //Create a new camera object and set the following properties of the camera object.
                            camera = new IPCamera
                            {
                                CameraName = ipArr[2],
                                Width      = int.Parse(ipArr[3]),
                                Height     = int.Parse(ipArr[4]),
                                DateTime   = Convert.ToDateTime(ipArr[5]),
                                FrameSize  = int.Parse(ipArr[6])
                            };
                            //set the size of the array to the temporary byte array
                            tempJPEGArray = new byte[sizeOfPayload];
                            tempJPEGArray = ReadPayload(tempJPEGArray, stream);          //Load the corresponding image
                            decodedData   = UTF8Encoding.ASCII.GetString(tempJPEGArray); //decode the data
                            DisplayImage(camera, tempJPEGArray);                         //Load image
                            bufferString = String.Empty;                                 //set it to an empty string to clear the contents for the new ip header.
                            break;

                        default:
                            bufferString += tempChar;
                            break;
                        }
                    }
                }
                else
                {
                    stream.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Something Went Wrong \n\n" + ex.Message);
                client.Close();
            }
        }