My goal is to draw things with opentk in c# on a GL_Control (a gui control) AND also save it to a Bitmap object everytime the paint event is called. I have this code:
private void glControl1_Paint(object sender, PaintEventArgs e)
{
// do lots of drawing here
GL.Finish();
GL.Flush();
glControl1.SwapBuffers();
gl_image = TakeScreenshot();
}
public Bitmap TakeScreenshot()
{
if (GraphicsContext.CurrentContext == null)
throw new GraphicsContextMissingException();
int w = glControl1.ClientSize.Width;
int h = glControl1.ClientSize.Height;
Bitmap bmp = new Bitmap(w, h);
System.Drawing.Imaging.BitmapData data =
bmp.LockBits(glControl1.ClientRectangle, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
GL.ReadPixels(0, 0, w, h, PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0);
bmp.UnlockBits(data);
bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
return bmp;
}
so inside the paint event after swaping the buffer, I take the schreenshot. The problem is that the captured image is of the state before the drawing. That means if I want to capture the image, I need to run the paint event twice. I've tried GL.flush and finish and swapbuffer. Does any one know how to overcome this problem. Also note that I've tried using an asynchronous way but that failed because you cannot access the opentk image data from another thread.