我的窗口表单包含aforge播放器和opengl控件。我正在使用aforge播放器播放网络摄像头。它的每个框架都使用opentk传递给glcontrol。它工作正常。现在,我想在aforge播放器上画一条线。我已经做了如下。但是,它在LoadTexture()上引发异常;
System.InvalidOperationException:“位图区域已被锁定。”
当我删除opengl的代码时,可以在aforge播放器上看到一行。因此,问题可能是将这个bitamp帧传递给了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);
}
}