View Full Version : OpenGL screenShot a game

04-25-2013, 06:55 AM
Hello everybody,

I need some help!

I have this code in c#:

// Creates a 1.0-compatible GraphicsContext with GraphicsMode.Default
GameWindow window = new GameWindow();

// Creates a 3.0-compatible GraphicsContext with 32bpp color, 24bpp depth
// 8bpp stencil and 4x anti-aliasing.
GLControl control = new GLControl(new GraphicsMode(32, 24, 8, 4), 3, 0, 0);

public Bitmap GrabScreenshot()
if (GraphicsContext.CurrentContext == null)
throw new GraphicsContextMissingException();

Bitmap bmp = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
System.Drawing.Imaging.BitmapData data =
bmp.LockBits(this.ClientRectangle, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb) ;
GL.ReadPixels(0, 0, this.ClientSize.Width, this.ClientSize.Height, PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0);

return bmp;

public void framebuffer()
const int FboWidth = 512;
const int FboHeight = 512;

uint FboHandle;
uint ColorTexture;
uint DepthRenderbuffer;

// Create Color Texture
GL.GenTextures(1, out ColorTexture);
GL.BindTexture(TextureTarget.Texture2D, ColorTexture);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Clamp);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Clamp);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, FboWidth, FboHeight, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);

// test for GL Error here (might be unsupported format)

GL.BindTexture(TextureTarget.Texture2D, 0); // prevent feedback, reading and writing to the same image is a bad idea

// Create Depth Renderbuffer
GL.Ext.GenRenderbuffers(1, out DepthRenderbuffer);
GL.Ext.BindRenderbuffer(RenderbufferTarget.Renderb ufferExt, DepthRenderbuffer);
GL.Ext.RenderbufferStorage(RenderbufferTarget.Rend erbufferExt, (RenderbufferStorage)All.DepthComponent32, FboWidth, FboHeight);

// test for GL Error here (might be unsupported format)

// Create a FBO and attach the textures
GL.Ext.GenFramebuffers(1, out FboHandle);
GL.Ext.BindFramebuffer(FramebufferTarget.Framebuff erExt, FboHandle);
GL.Ext.FramebufferTexture2D(FramebufferTarget.Fram ebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, ColorTexture, 0);
GL.Ext.FramebufferRenderbuffer(FramebufferTarget.F ramebufferExt, FramebufferAttachment.DepthAttachmentExt, RenderbufferTarget.RenderbufferExt, DepthRenderbuffer);

// now GL.Ext.CheckFramebufferStatus( FramebufferTarget.FramebufferExt ) can be called, check the end of this page for a snippet.

// since there's only 1 Color buffer attached this is not explicitly required
GL.DrawBuffer((DrawBufferMode)FramebufferAttachmen t.ColorAttachment0Ext);

GL.PushAttrib(AttribMask.ViewportBit); // stores GL.Viewport() parameters
GL.Viewport(0, 0, FboWidth, FboHeight);

// render whatever your heart desires, when done ...

GL.PopAttrib(); // restores GL.Viewport() parameters
GL.Ext.BindFramebuffer(FramebufferTarget.Framebuff erExt, 0); // return to visible framebuffer
DrawBuffersEnum[] bufs = new DrawBuffersEnum[2] { (DrawBuffersEnum)FramebufferAttachment.ColorAttach ment0Ext, (DrawBuffersEnum)FramebufferAttachment.ColorAttach ment1Ext }; // fugly, will be addressed in 0.9.2

GL.DrawBuffers(bufs.Length, bufs);

private void button1_Click(object sender, EventArgs e)
pictureBox1.Image = GrabScreenshot();

But the screenshot still black, someone can help me ?

thanks! This is from OpenTK librarie!

04-26-2013, 11:05 AM
help me please!

04-27-2013, 02:46 AM
I don't program in C# but it might be a problem with which render buffer is bound when you grab the data.

04-29-2013, 08:37 AM
i try inject this on counter strike, but give me an error, null exepcion