简体   繁体   English

C#崩溃,显示“参数无效”。 在Application.Run(new Control_Panel())中将PictureBox.image设置为Bitmap时

[英]C# crashes with 'Parameter is not valid.' when setting PictureBox.image to Bitmap in Application.Run(new Control_Panel())

After I connect to USB camera, and read a frame, and convert to bitmap, it crashes after I write bitmap to PictureBox. 当我连接到USB摄像头并读取框架并转换为位图后,将位图写入PictureBox后崩溃。

I'm developing a Visual Studio 2017 Pro C# Windows forms project. 我正在开发一个Visual Studio 2017 Pro C#Windows窗体项目。

Also, if I debug process_video_NewFrame() and step through it, then crash 'Parameter is not valid.' 另外,如果我调试process_video_NewFrame()并逐步执行,则崩溃“参数无效”。 occurs at line in Program.cs: Application.Run(new Control_Panel()). 发生在Program.cs中的行:Application.Run(new Control_Panel())。

Control_Panel.cs 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 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());
        }



    }
}

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 );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM