I want to read a texture pixels from texture id (associated with some FBO) to bitmap object in opengl (opentk) in c#
But it throws an exception says:
System.AccessViolationException occurred
HResult=0x80004003
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=OpenTK
The code:
public Bitmap GetTexture(int AttachmentIndex)
{
if (AttachmentIndex > fbo_colorBuffer.Count - 1)
throw new IndexOutOfRangeException();
Bitmap bitmap = new Bitmap(width, height);
BitmapData bits = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
BindToRead(ReadBufferMode.ColorAttachment0 + AttachmentIndex);
GL.ReadPixels(0, 0, width, height, OpenTK.Graphics.OpenGL.PixelFormat.Rgb, PixelType.Float, bits.Scan0);
bitmap.UnlockBits(bits);
return bitmap;
}
public void BindToRead(ReadBufferMode Mode)
{
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, fbo_id);
GL.ReadBuffer(Mode);
}