OpenGL Screen Grab

Some years ago I found this code in the forum. I use it to create a PNG files not from the screen but from the OpnGLSurface control.
It has worked without problems so far.

[code]Function getPicture(glSurf As OpenGLSurface) As Picture

'Capture OpenGLSurface image and puts it into a PNG file

//calculate sizes
dim w As integer = glSurf.Width
dim h As integer = glSurf.Height
dim pad As integer = w mod 4
dim totalBytes As integer = (w * h * 3) + (h * pad)

//create and fill memoryblock with pixel data
glSurf.MakeCurrent
dim m As MemoryBlock = New MemoryBlock(totalBytes)
OpenGL.glReadPixels(0, 0, w, h, OpenGL.GL_BGR, OpenGL.GL_UNSIGNED_BYTE, m)

//create output picture
dim p As Picture = New Picture(w, h, 32)
dim surf As RGBSurface = p.RGBSurface

//scan memoryblock into picture
h = h - 1
w = w - 1
dim x, y, offset As integer //offset starts at 0
for y = h downto 0
for x = 0 to w
surf.Pixel(x, y) = m.ColorValue(offset, 24)
offset = offset + 3
next
offset = offset + pad //skip per line
next

return p //store picture

Exception
LOG "Error: " + CurrentMethodName

End Function[/code]