Рисование линии в видеопотоке показывает: «Область растрового изображения уже заблокирована». - PullRequest
0 голосов
/ 03 декабря 2018

Моя форма окна содержит aforge player и элемент управления opengl.Я играю в веб-камеру, используя aforge player.Каждый его кадр передается в glcontrol с помощью opentk.это работает нормально.Теперь я хочу нарисовать линию на aforge player.Я сделал, как показано ниже.Но это вызывает исключение для LoadTexture ();

System.InvalidOperationException: 'Область растрового изображения уже заблокирована.'

Когда я удаляю коды opengl, в aforge player появляется строка.Таким образом, проблема может заключаться в передаче этого битового кадра в opengl.Пожалуйста, помогите решить проблему.

Мой код здесь:

private void StartCameras()
{
    // create first video source
    VideoCaptureDevice videoSource1 = new VideoCaptureDevice(videoDevices[camera1Combo.SelectedIndex].MonikerString);
    videoSource1.DesiredFrameRate = 10;
    videoSourcePlayer1.VideoSource = videoSource1;
    videoSourcePlayer1.Start();     
}
private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)
    {
        videoFrame = image;
        DrawLineInt(videoFrame);
    }
private void Application_Idle(object sender, EventArgs e)
    {
        while (glControl1.IsIdle)
        {
            Render();
        }
    }
public void Render()
    {
        GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); 
        if (videoFrame != null)
            lock (videoFrame)
            {
                if (videoTexture != -1)
                GL.DeleteTextures(1, ref videoTexture);
                videoTexture = LoadTexture(videoFrame);
                videoFrame.Dispose();
                videoFrame = null;
            }
        GC.Collect();
        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
        DrawImage(videoTexture);
    }
public int LoadTexture(Bitmap bitmap)
    {
        int tex = -1;
        if (bitmap != null)
        {
           GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
           GL.GenTextures(1, out tex);
           GL.BindTexture(TextureTarget.Texture2D, tex);
           bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
           BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
           ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
            OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
            bitmap.UnlockBits(data);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
        }
        return tex;
    }
public void DrawLineInt(Bitmap bmp)
{
    Pen blackPen = new Pen(Color.Black, 3);
    int x1 = 100;
    int y1 = 100;
    int x2 = 500;
    int y2 = 100;
    // Draw line to screen.
    using (var graphics = Graphics.FromImage(bmp))
    {
        graphics.DrawLine(blackPen, x1, y1, x2, y2);
    }
}
...