private void WorkerThread( )
{
int width = region.Width;
int height = region.Height;
int x = region.Location.X;
int y = region.Location.Y;
System.Drawing.Size size = region.Size;
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);
// download start time and duration
DateTime start;
TimeSpan span;
while ( !stopEvent.WaitOne( 0, false ) )
{
// set dowbload start time
start = DateTime.Now;
try
{
// capture the screen
graphics.CopyFromScreen(x, y, 0, 0, size, System.Drawing.CopyPixelOperation.SourceCopy);
// increment frames counter
framesReceived++;
// provide new image to clients
if ( NewFrame != null )
{
// notify client
NewFrame( this, new NewFrameEventArgs( bitmap ) );
}
// wait for a while ?
if ( frameInterval > 0 )
{
// get download duration
span = DateTime.Now.Subtract( start );
// miliseconds to sleep
int msec = frameInterval - (int) span.TotalMilliseconds;
if ( ( msec > 0 ) && ( stopEvent.WaitOne( msec, false ) ) )
break;
}
}
catch ( ThreadAbortException )
{
break;
}
catch ( Exception exception )
{
// provide information to clients
if ( VideoSourceError != null )
{
VideoSourceError( this, new VideoSourceErrorEventArgs( exception.Message ) );
}
// wait for a while before the next try
Thread.Sleep( 250 );
}
// need to stop ?
if ( stopEvent.WaitOne( 0, false ) )
break;
}
// release resources
graphics.Dispose( );
bitmap.Dispose( );
if ( PlayingFinished != null )
{
PlayingFinished( this, ReasonToFinishPlaying.StoppedByUser );
}
}
}