예제 #1
0
        private void sendStreamMjpegFromFile(string fileName, NetworkStream ns, int SendBufferSize, int framRate, Size videoSize, int videoQuality)
        {
            DateTime start;
            TimeSpan span;

            int frameInterval = 1000 / framRate;

            VsDexterLibVideoReader fe = new VsDexterLibVideoReader(fileName, videoSize, frameInterval);

            // VeWmvLibVideoReader fe = new VeWmvLibVideoReader(fileName, frameInterval,videoSize);
            #region setImageEncoder
            // Get a bitmap.
            // Bitmap bmp1 = new Bitmap(@"c:\TestPhoto.jpg");

            ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);

            // Create an Encoder object based on the GUID
            // for the Quality parameter category.
            System.Drawing.Imaging.Encoder myEncoder =
                System.Drawing.Imaging.Encoder.Quality;

            // Create an EncoderParameters object.
            // An EncoderParameters object has an array of EncoderParameter
            // objects. In this case, there is only one
            // EncoderParameter object in the array.
            EncoderParameters myEncoderParameters = new EncoderParameters();

            // Save the bitmap as a JPG file with zero quality level compression.
            EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, (long)videoQuality);
            myEncoderParameters.Param[0] = myEncoderParameter;
            #endregion

            MemoryStream stream;
            Bitmap       bmp;
            //double i = 0;
            while (fe.getNextFrameAsInterval(out bmp))
            {
                stream = new MemoryStream();
                start  = DateTime.Now;

                // bmp = fe.GetFrameFromVideoPosition(i);
                //Form1.bmp = new Bitmap(bmp);
                // = new Bitmap(bmp videoSize)

                //bmp.SetResolution(videoSize.Width, videoSize.Height);
                bmp.Save(stream, jgpEncoder, myEncoderParameters);
                sendMjpegHead(ns, stream.Length);
                //bmp.Save(ns, jgpEncoder, myEncoderParameters);
                //Bitmap.FromStream(stream).Save(ns,ImageFormat.Jpeg);

                sendStream(ns, SendBufferSize, stream);

                byte[] bHeadersString = Encoding.ASCII.GetBytes("\r\n");
                // Send headers
                ns.Write(bHeadersString, 0, bHeadersString.Length);

                stream.Close();
                // ;
                //i += 1.0 / framRate;
                //if (i > fe.streamLength)
                //{
                //    break;
                //}

                span = DateTime.Now.Subtract(start);
                // miliseconds to sleep
                int msec = fe.outputFrameInterval - (int)span.TotalMilliseconds;

                while ((msec > 0))
                {
                    // sleeping ...
                    Thread.Sleep((msec < 100) ? msec : 100);
                    msec -= 100;
                }
            }
            fe.Close();
        }
예제 #2
0
        private void sendStreamMjpegFromFile(string fileName, NetworkStream ns, int SendBufferSize, int framRate, Size videoSize, int videoQuality)
        {
            DateTime start;
            TimeSpan span;

            int frameInterval = 1000 / framRate;

            VsDexterLibVideoReader fe = new VsDexterLibVideoReader(fileName, videoSize,frameInterval);
           // VeWmvLibVideoReader fe = new VeWmvLibVideoReader(fileName, frameInterval,videoSize);
            #region setImageEncoder
            // Get a bitmap.
            // Bitmap bmp1 = new Bitmap(@"c:\TestPhoto.jpg");

            ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);

            // Create an Encoder object based on the GUID
            // for the Quality parameter category.
            System.Drawing.Imaging.Encoder myEncoder =
                System.Drawing.Imaging.Encoder.Quality;

            // Create an EncoderParameters object.
            // An EncoderParameters object has an array of EncoderParameter
            // objects. In this case, there is only one
            // EncoderParameter object in the array.
            EncoderParameters myEncoderParameters = new EncoderParameters();

            // Save the bitmap as a JPG file with zero quality level compression.
            EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, (long)videoQuality);
            myEncoderParameters.Param[0] = myEncoderParameter;
            #endregion

            MemoryStream stream;
            Bitmap bmp;
            //double i = 0;
            while (fe.getNextFrameAsInterval(out bmp))
            {
                stream = new MemoryStream();
                start = DateTime.Now;

                // bmp = fe.GetFrameFromVideoPosition(i);
                //Form1.bmp = new Bitmap(bmp);
                // = new Bitmap(bmp videoSize)
               
                //bmp.SetResolution(videoSize.Width, videoSize.Height);
                bmp.Save(stream, jgpEncoder, myEncoderParameters);
                sendMjpegHead(ns, stream.Length);
                //bmp.Save(ns, jgpEncoder, myEncoderParameters);
                //Bitmap.FromStream(stream).Save(ns,ImageFormat.Jpeg);

                sendStream(ns, SendBufferSize, stream);

                byte[] bHeadersString = Encoding.ASCII.GetBytes("\r\n");
                // Send headers	
                ns.Write(bHeadersString, 0, bHeadersString.Length);

                stream.Close();
                // ;
                //i += 1.0 / framRate;
                //if (i > fe.streamLength)
                //{
                //    break;
                //}

                span = DateTime.Now.Subtract(start);
                // miliseconds to sleep
                int msec = fe.outputFrameInterval - (int)span.TotalMilliseconds;

                while ((msec > 0))
                {
                    // sleeping ...
                    Thread.Sleep((msec < 100) ? msec : 100);
                    msec -= 100;
                }
            }
            fe.Close();

        }