0

After I connect to USB camera, and read a frame, and convert to bitmap, it crashes after I write bitmap to PictureBox.

I'm developing a Visual Studio 2017 Pro C# Windows forms project.

Also, if I debug process_video_NewFrame() and step through it, then crash 'Parameter is not valid.' occurs at line in Program.cs: Application.Run(new Control_Panel()).

Control_Panel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


using System.Drawing;

using Accord.Video;
using Accord.Video.DirectShow;

namespace ACCORD_WindowsFormsApp9
{
    public partial class Control_Panel : Form
    {
        VideoCaptureDevice videoSource;
        Bitmap bitmap;



        public Control_Panel()
        {
            InitializeComponent();
        }

        private void button_Start_Frame_Captcha_Click(object sender, EventArgs e)
        {
            var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
            videoSource.NewFrame += new NewFrameEventHandler(process_video_NewFrame);
            videoSource.Start();
        }




        // The video_NewFrame used in the example above could be defined as:
        private  void process_video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            // get new frame
            bitmap = eventArgs.Frame;
            System.Windows.Forms.Application.DoEvents();

            this.pictureBoxLatestCameraFrame.Image = bitmap;
        }



        private void button_Stop_Frame_Captcha_Click(object sender, EventArgs e)
        {
            videoSource.SignalToStop();
        }

    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ACCORD_WindowsFormsApp9
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Control_Panel());
        }



    }
}
Doug Null
  • 7,989
  • 15
  • 69
  • 148
  • 1
    My crystal ball says that `eventArgs.Frame` gets disposed after the event is completed. Create new bitbap out of existing image and set it to picture box. – Sriram Sakthivel Apr 05 '18 at 19:06
  • Can you post the code in your InitializeComponent() method? – Katianie Apr 05 '18 at 19:10
  • Sriram's ball is likely to be accurate. Another ball says that the event runs on a worker thread, so assigning the Image property is not safe either. – Hans Passant Apr 05 '18 at 19:23

1 Answers1

0

Fixed! Thanks, Sriram. Keep that crystal ball polished... New to Bitmap, I mistakenly assumed it was an image, when actually it is an object, and so requires a new instance each iteration, ie...

private  void process_video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
    // Ax previous Bitmap object:
    if( loops > 0 )
    {   
         camera_snapshot_bitmap.Dispose();
    }

    // get new frame
    camera_snapshot_bitmap = new Bitmap( eventArgs.Frame );
    //ax System.Windows.Forms.Application.DoEvents();

    this.pictureBoxLatestCameraFrame.Image = camera_snapshot_bitmap;
    ++loops;
    Console.WriteLine("Loops " + loops );
}
Doug Null
  • 7,989
  • 15
  • 69
  • 148