CsGL + Saving Image to Disk?

  • Thread starter Thread starter illmatic
  • Start date Start date
I

illmatic

Can someone post or give me a link to C# code that will allow me to
save what is currently being shown in my CsGL control to disk? Any
format will do, though bmp, jpg, or gif is preferred.

Thanks!
 
illmatic said:
Can someone post or give me a link to C# code that will allow me to
save what is currently being shown in my CsGL control to disk? Any
format will do, though bmp, jpg, or gif is preferred.

Bleh! Bleh!
 
illmatic said:
Can someone post or give me a link to C# code that will allow me to
save what is currently being shown in my CsGL control to disk? Any
format will do, though bmp, jpg, or gif is preferred.

OpenGL has a glReadPixels that you can use to get the pixels that are shown
on the screen.
I haven't tried it, but if you initialize a Bitmap instance with the right
size and type [ie. RGBA] and then call the LockBits method, you may be able
to pass the Scan0 pointer of the BitmapData class directly to the
glReadPixels method as the pixels parameter. The only thing you have to do
then is to unlock the bits and call the Save method to save your image in
one of the supported formats.

Regards,
Pieter Philippaerts
Managed SSL/TLS: http://www.mentalis.org/go.php?sl
 
Pieter Philippaerts said:
OpenGL has a glReadPixels that you can use to get the pixels that are shown
on the screen.

Here's some code that works, apart from the fact that the Bitmap is flipped:

Bitmap b = new Bitmap(width, height, PixelFormat.Format32bppArgb);
BitmapData bd = b.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
glReadPixels(0, 0, 1600, 1200, GL_BGRA_EXT, GL_UNSIGNED_BYTE, bd.Scan0);
b.UnlockBits(bd);
b.Save(@"c:\test.bmp", ImageFormat.Bmp);

You can call the b.RotateFlip method to flip the bitmap before saving.

Regards,
Pieter Philippaerts
Managed SSL/TLS: http://www.mentalis.org/go.php?sl

P.S.: for more information about glReadPixels, check out
http://msdn.microsoft.com/library/en-us/opengl/glfunc03_8m7n.asp
 
I updated your code to follow CsGL syntax:

public void saveScene()
{
Size s = Size;

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, 1600, 1200, GL.GL_BGRA_EXT,
GL.GL_UNSIGNED_BYTE, bd.Scan0);
b.UnlockBits(bd);
//b.RotateFlip();
b.Save(@"c:\test.bmp", ImageFormat.Bmp);
}

However, when I run this code, my computer hard resets! It’s the
strangest thing. Can you point out what I might be doing wrong?
Thanks…
 
I hard coded the resolution while testing the code, but apparently I forgot
to change one of the lines before posting it here.
Try replacing the 1600 and 1200 in the call to glReadPixels with 'width' and
'height' respectively.

Regards,
Pieter Philippaerts
Managed SSL/TLS: http://www.mentalis.org/go.php?sl
 
Back
Top