首页 > 解决方案 > AForge.Video / Materialskin 反复导致stackoverflow

问题描述

好的,我正在使用 Materialskin 和 AForge 制作一个很好的应用程序,它可以有相机预览,在图片框中显示它们,然后可以保存。

这是我认为导致错误的代码部分

        VideoCaptureDevice videoSource;

        private FilterInfoCollection VideoCaptureDevices;
        private VideoCaptureDevice FinalVideo;
        int imgCropHeight = 300;
        int imgCropWidth = 500;
        int imgCropX = 400;
        int imgCropY = 300;
        bool isCameraRunning = true;
        public async void AForgeLoadDevice()
        {
            VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
            {
                cameraList.Items.Add(VideoCaptureDevice.Name);
            }
            cameraList.SelectedIndex = 0;
            await Task.Delay(90);
        }

        public void AForgeOpen()
        {
            try
            {
                FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[cameraList.SelectedIndex].MonikerString);
                FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
                FinalVideo.Start();
            } catch(Exception err)
            {
                MessageBox.Show(err.ToString());
            }
        }

        public void AForgeClose()
        {
            FinalVideo.Stop();
        }


        async void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap video = (Bitmap)eventArgs.Frame.Clone();
            Crop crop = new Crop(new Rectangle(imgCropX, imgCropY, imgCropWidth, imgCropHeight));
            Bitmap newImage = crop.Apply(video);
            pictureBox1.Image = newImage;
            await Task.Delay(50);
        }

我曾尝试在创建新表单时使用块,但这并没有解决任何问题。我从未在 Visual Studio 中收到过 stackoverflow 错误,所以我不太了解它,但我只知道它说它必须是由于 AForge / MaterialSkin 之类的框架而发生的

有什么修复吗?这真的很有帮助。

我认为它与 NewFrameEvent 处理程序有关,因为它确实是导致堆栈溢出的唯一方法

编辑:这通常发生在保持表单打开很长时间(1 分钟以上)或关闭并重新创建(我有一个 onclose 事件会停止伪造视频源)之后

标签: c#winformsaforge

解决方案


推荐阅读