AVI files reading using Video for Windows.

The class allows to read AVI files using Video for Windows API.

Sample usage:

// instantiate AVI reader AVIReader reader = new AVIReader( ); // open video file reader.Open( "test.avi" ); // read the video file while ( reader.Position - reader.Start < reader.Length ) { // get next frame Bitmap image = reader.GetNextFrame( ); // .. process the frame somehow or display it } reader.Close( );
Inheritance: IDisposable
        /// <summary>
        /// Worker thread.
        /// </summary>
        /// 
        private void WorkerThread( )
		{
            ReasonToFinishPlaying reasonToStop = ReasonToFinishPlaying.StoppedByUser;
            // AVI reader
			AVIReader aviReader = new AVIReader( );

			try
			{
				// open file
				aviReader.Open( source );

                // stop positions
                int stopPosition = aviReader.Start + aviReader.Length;

                // frame interval
                int interval = ( frameIntervalFromSource ) ? (int) ( 1000 / aviReader.FrameRate ) : frameInterval;

                while ( !stopEvent.WaitOne( 0, false ) )
				{
					// start time
					DateTime start = DateTime.Now;

					// get next frame
					Bitmap bitmap = aviReader.GetNextFrame( );

					framesReceived++;
                    bytesReceived += bitmap.Width * bitmap.Height *
                        ( Bitmap.GetPixelFormatSize( bitmap.PixelFormat ) >> 3 );

					if ( NewFrame != null )
						NewFrame( this, new NewFrameEventArgs( bitmap ) );

					// free image
					bitmap.Dispose( );

                    // check current position
                    if ( aviReader.Position >= stopPosition )
                    {
                        reasonToStop = ReasonToFinishPlaying.EndOfStreamReached;
                        break;
                    }

                    // wait for a while ?
                    if ( interval > 0 )
                    {
                        // get frame extract duration
                        TimeSpan span = DateTime.Now.Subtract( start );

                        // miliseconds to sleep
                        int msec = interval - (int) span.TotalMilliseconds;

                        if ( ( msec > 0 ) && ( stopEvent.WaitOne( msec, false ) ) )
                            break;
                    }
				}
			}
			catch ( Exception exception )
			{
                // provide information to clients
                if ( VideoSourceError != null )
                {
                    VideoSourceError( this, new VideoSourceErrorEventArgs( exception.Message ) );
                }
			}

			aviReader.Dispose( );
			aviReader = null;

            if ( PlayingFinished != null )
            {
                PlayingFinished( this, reasonToStop );
            } 
		}
        /// <summary>
        /// Worker thread.
        /// </summary>
        ///
        private void WorkerThread( )
        {
            ReasonToFinishPlaying reasonToStop = ReasonToFinishPlaying.StoppedByUser;
            // AVI reader
            AVIReader aviReader = new AVIReader( );

            try
            {
                // open file
                aviReader.Open(source);

                // stop positions
                int stopPosition = aviReader.Start + aviReader.Length;

                // frame interval
                int interval = (frameIntervalFromSource) ? (int)(1000 / aviReader.FrameRate) : frameInterval;

                while (!stopEvent.WaitOne(0, false))
                {
                    // start time
                    DateTime start = DateTime.Now;

                    // get next frame
                    Bitmap bitmap = aviReader.GetNextFrame( );

                    framesReceived++;
                    bytesReceived += bitmap.Width * bitmap.Height *
                                     (Bitmap.GetPixelFormatSize(bitmap.PixelFormat) >> 3);

                    if (NewFrame != null)
                    {
                        NewFrame(this, new NewFrameEventArgs(bitmap));
                    }

                    // free image
                    bitmap.Dispose( );

                    // check current position
                    if (aviReader.Position >= stopPosition)
                    {
                        reasonToStop = ReasonToFinishPlaying.EndOfStreamReached;
                        break;
                    }

                    // wait for a while ?
                    if (interval > 0)
                    {
                        // get frame extract duration
                        TimeSpan span = DateTime.Now.Subtract(start);

                        // miliseconds to sleep
                        int msec = interval - (int)span.TotalMilliseconds;

                        if ((msec > 0) && (stopEvent.WaitOne(msec, false)))
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                // provide information to clients
                if (VideoSourceError != null)
                {
                    VideoSourceError(this, new VideoSourceErrorEventArgs(exception.Message));
                }
            }

            aviReader.Dispose( );
            aviReader = null;

            if (PlayingFinished != null)
            {
                PlayingFinished(this, reasonToStop);
            }
        }