View Full Version : [HELP]Getting a black screen(C#-GL.glReadPixels)

07-24-2009, 03:36 PM
Hello everyone,
I have 2 codes (I found in here/internet) that suppost to copy the pixels from my screen (By the OpenGL lib) into a Bitmap class.
The problem is that both of them print a black screen.

The codes:

static Bitmap GrabScreenshot()
Bitmap bmp = new Bitmap(800, 600);
System.Drawing.Imaging.BitmapData data = bmp.LockBits(new Rectangle(0, 0, 800, 600), System.Drawing.Imaging.ImageLockMode.WriteOnly,
System.Drawing.Imaging.PixelFormat.Format24bppRgb) ;
GL.glReadPixels(0, 0, 800, 600, GL.GL_BGR, GL.GL_UNSIGNED_BYTE, data.Scan0);
return bmp;

static Bitmap GrabScreenshot()
Size s = new Size();
s.Width = 800;
s.Height = 600;
Bitmap b = new Bitmap(s.Width, s.Height, PixelFormat.Format32bppArgb);
BitmapData bd = b.LockBits(new Rectangle(0, 0, s.Width, s.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
GL.glReadPixels(0, 0, s.Width, s.Height, GL.GL_BGRA_EXT, GL.GL_UNSIGNED_BYTE, bd.Scan0);
return b

My main idea is to capture a screen when you are in a 3D game (Like: Counter-Strike/Counter-Strike:Source),
and few people told me that the solution for it is using OpenGL,

Thanks in advanced,

Stephen A
07-25-2009, 01:25 AM
The code is correct: this will capture the contents of an OpenGL context into a System.Drawing.Bitmap.

However, the context will have to be current on the thread you are calling GrabScreenshot() from. If the context is in another process, this code is not going to work.

07-25-2009, 01:53 AM
I think it is related to this recent thread:
http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=261160#Post2611 60

Does that help you?