Смешивание видео на C#

#c# #opencv #image-processing

Вопрос:

Я смешиваю видео с приведенным ниже кодом

 var capture = new VideoCapture(path1);
                var capture2 = new VideoCapture(path2);
                // Frame image buffer
                Mat image = new Mat();
                Mat image2 = new Mat();

                // When the movie playback reaches end, Mat.data becomes NULL.
                while (videoStreamFlag)
                {
                    capture2.Read(image2);
                    capture.Read(image); 
                    if (image.Empty() || image2.Empty())
                    {
                        videoStreamFlag = false;
                        break;
                    }
                    bitimg = new Bitmap(pictureBox.Width, pictureBox.Height, ((image.Width * 3   3) / 4) * 4, PixelFormat.Format24bppRgb, image.Data);
                    bitimg2 = new Bitmap(pictureBox.Width, pictureBox.Height, ((image2.Width * 3   3) / 4) * 4, PixelFormat.Format24bppRgb, image2.Data);

                    bitimg2 = SetImageOpacity(bitimg2, (float)50 / 100f);

                    result = new Bitmap(Math.Max(bitimg.Width, bitimg2.Width), Math.Max(bitimg.Height, bitimg2.Height));
                    g = Graphics.FromImage(result);

                    g.DrawImageUnscaled(bitimg, 0, 0);
                    g.DrawImageUnscaled(bitimg2, 0, 0);

                    Thread.Sleep(1);
                    pictureBox.Image = resu<
                    Thread.Sleep(1);

                    GC.SuppressFinalize(this);
                    GC.Collect();
                }
 
 public Bitmap SetImageOpacity(Image image, float opacity)
        {
            try
            {
                //create a Bitmap the size of the image provided  
                bmp = new Bitmap(image.Width, image.Height);
                //create a graphics object from the image  
                Graphics gfx = Graphics.FromImage(bmp);
                //create a color matrix object  
                ColorMatrix matrix = new ColorMatrix();
                //set the opacity  
                matrix.Matrix33 = opacity;
                //create image attributes  
                ImageAttributes attributes = new ImageAttributes();
                //set the color(opacity) of the image  
                attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                //now draw the image  
                gfx.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);

                return bmp;
            }
            catch
            {
                return null;
            }
        }
 

Если размер PictureBox равен 1920×1080, я получаю сообщение об ошибке. Если это 1280×720, мой код работает нормально.

Я сталкиваюсь со следующей ошибкой в коде gfx.Drawimage в функции SetImageOpacity:Система.Исключение AccessViolationException: «Попытка чтения или записи защищенной памяти. Это часто указывает на то, что другая память повреждена. » Как я могу решить эту проблему. Большое вам спасибо за вашу помощь.