Или, должен ли pictureBox обновляться только потоком пользовательского интерфейса?
Я назначаю функцию обратного вызова для моего класса Accord VideoCaptureDevice, который получает живые кадры с моей USB-камеры. Около 10 кадров в секунду.
В обратном вызове события я обновляю pictureBox с последним живым кадром.
Я пытаюсь выяснить, почему я получаю эту прерывистую ошибку ...
«… Необработанное исключение типа« System.ArgumentException »произошло в System.Drawing.dll
Параметр недействителен. "
КОД, КОТОРЫЙ НАЧИНАЕТ СОБЫТИЕ КАМЕРЫ ...
// Automatically start inputing each frame from camera:
var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
// Select physical USB camera: http://accord-framework.net/docs/html/T_Accord_Video_DirectShow_VideoCaptureDevice.htm
videoSource = new VideoCaptureDevice( videoDevices[0].MonikerString, pixelFormat_for_ExhaustiveTemplateMatching);
Console.WriteLine( "BOOT: PixelFormat = " + pixelFormat_for_ExhaustiveTemplateMatching.ToString() );
// Select event handlers:
videoSource.NewFrame += new NewFrameEventHandler( EVENT_camera_frame ); //http://accord-framework.net/docs/html/T_Accord_Video_NewFrameEventArgs.htm
// Start slurpin up them camera frames:
videoSource.Start();
КОД СОБЫТИЙ, КОТОРЫЙ ПОЛУЧАЕТ РАМКУ, НАБИРАЕТ НА КОРОБКЕ И ПОКАЗЫВАЕТ PICTUREBOX ...
if( latest_frame_buffer_Bitmap != null )
{
latest_frame_buffer_Bitmap.Dispose();
}
// Convert from camera native pixel format to comparison compatible format:
latest_frame_buffer_Bitmap = new Bitmap( camera_frame_Bitmap.Width,
camera_frame_Bitmap.Height,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
using (Graphics gr = Graphics.FromImage( latest_frame_buffer_Bitmap))
{
gr.DrawImage( camera_frame_Bitmap, new Rectangle( 0, 0,
latest_frame_buffer_Bitmap.Width,
latest_frame_buffer_Bitmap.Height));
// Apply user specified angle of rotation:
switch( image_rotation_degrees )
{
case 0: latest_frame_buffer_Bitmap.RotateFlip( RotateFlipType.RotateNoneFlipNone ); break;
case 90: latest_frame_buffer_Bitmap.RotateFlip( RotateFlipType.Rotate90FlipNone ); break;
case 180: latest_frame_buffer_Bitmap.RotateFlip( RotateFlipType.Rotate180FlipNone ); break;
case 270: latest_frame_buffer_Bitmap.RotateFlip( RotateFlipType.Rotate270FlipNone ); break;
}
Rectangle crop_rectangle = new Rectangle( 0,0,0,0);
get_crop_rectangle( PictureBox_live_camera.Width,
PictureBox_live_camera.Height,
ref crop_rectangle ); // latest_frame_buffer_Bitmap ==> crop_rectangle X, Y, Width, Height
if( Computer_Vision_API.reference_cropped_Bitmap != null )
{
Computer_Vision_API.reference_cropped_Bitmap.Dispose();
}
Computer_Vision_API.reference_cropped_Bitmap = new Bitmap( Computer_Vision_API.latest_frame_buffer_Bitmap );
Computer_Vision_API.reference_cropped_Bitmap = Computer_Vision_API.latest_frame_buffer_Bitmap.Clone( crop_rectangle,
Computer_Vision_API.latest_frame_buffer_Bitmap.PixelFormat);
pictureBox_reference_snapshot.Image = Computer_Vision_API.reference_cropped_Bitmap;
// Draw crop rectangle in Live Camera View:
Graphics live_image_Graphics = Graphics.FromImage( latest_frame_buffer_Bitmap ) ;
live_image_Graphics.DrawRectangle( crop_pen, crop_display_rectangle ); ///////////// DRAW CROPPING RECTANGLE /////////////
live_image_Graphics.Dispose();
// Show crosshairs, if requested in camera view:
// Create pen.
Pen crosshairs_pen = new Pen(Color.White, 1 );
int width = Computer_Vision_API.latest_frame_buffer_Bitmap.Width;
int height = Computer_Vision_API.latest_frame_buffer_Bitmap.Height;
int top_y = height - 1;
int bottom_y = 0;
int left_x = 0;
int right_x = width - 1;
// Create points that define line.
Point crosshair_point_top = new Point( width / 2, top_y );
Point crosshair_point_bottom = new Point( width / 2, bottom_y );
Point crosshair_point_left = new Point( 0, height / 2 );
Point crosshair_point_right = new Point( width-1, height / 2 );
float[] dashValues = { 4, 2 };
crosshairs_pen.DashPattern = dashValues;
// Verticle crosshair:
Graphics live_image_Graphics = Graphics.FromImage( Computer_Vision_API.latest_frame_buffer_Bitmap );
live_image_Graphics.DrawLine( crosshairs_pen, crosshair_point_top, crosshair_point_bottom );
// Horizontal crosshair:
live_image_Graphics.DrawLine( crosshairs_pen, crosshair_point_left, crosshair_point_right );
live_image_Graphics.Dispose();
// Display latest frame from camera:
PictureBox_live_camera.Image = Computer_Vision_API.latest_frame_buffer_Bitmap;
PictureBox_live_camera.BackgroundImage = Computer_Vision_API.latest_frame_buffer_Bitmap;
} // END OF 'using'
КОНЕЦ СОБЫТИЙ