
Grabbing the rendered image as a Bitmap
Posted Friday, 29 February, 2008 - 05:21 by death_au inIs there an easy way to grab something rendered in a glControl and store it in a Bitmap or Image object?
This is the code I'm using now:
Bitmap^ thumb = gcnew Bitmap(glControl->Width,glControl->Height); Graphics^ gr = Graphics::FromImage(thumb); gr->Clear(Color::White); array<int>^ pixels = gcnew array<int>(3*thumb->Width*thumb->Height); GL::ReadPixel(0, 0, glControl->Width,glControl->Height, PixelFormat::Rgb, PixelType::Int, pixels); for(int y = 0; y < thumb->Height; y++) for(int x = 0; x < thumb->Width; x++) thumb->SetPixel(x,thumb->Height-y-1,Color::FromArgb(pixels[((thumb->Width*y )+ x)*3]));
I know the color interpretation is completely wrong, but before I fix that I'm wondering why I'm not getting the entire image. It grabs everything from the far left, to about 3/4 of the way to the right and cuts off there....
...This code is a mess because I've been trying different stuff all day, but it at least gives you an idea of where I'm at.


Comments
Re: Grabbing the rendered image as a Bitmap
It would be really nice to be able to write:
Bitmap bitmap = glControl.CreateBitmap(); // C#
:)
Re: Grabbing the rendered image as a Bitmap
Just added this method to GLControl:
Works perfectly here :)
Edit: Also fixed GL.ReadPixel, should have been GL.ReadPixels.
Re: Grabbing the rendered image as a Bitmap
Great, fast fix Fiddler! Does it work for both .NET and mono?
Re: Grabbing the rendered image as a Bitmap
Couldn't test on Mono, because I've broken window creation in SVN. Seeing that this code works for Bitmap loading however, it will probably for saving too.
I'll test it as soon as I can get window creation to work.