copying surface to canvas

I’m trying to copy a surface (opengl) to a canvas in order to get the picture. I don’t want to use screenshot as I want it to do this off screen.

dim pixeldata as new MemoryBlock(4)
dim p as picture
p=new Picture(90,90,32)
Surface.MakeCurrent
for x as integer = 1 to 90
for y as integer = 1 to 90
OpenGL.glReadPixels(x, y, 1, 1, OpenGL.GL_RGBA, OpenGL.GL_UNSIGNED_BYTE, pixelData)
p.Graphics.ForeColor=pixeldata.ColorValue(0,8)
p.Graphics.DrawLine(x,y,x+1,y+1)
next
next
Canvas1.Backdrop=p
p.save(image etc)

This seems to take forever for a small icon, and the image is always a white square, anyone any ideas whats going on?

This is from Will Shank’s (awesome) ezgl classes:

[code]Function getPicture(glSurf As OpenGLSurface) As Picture
//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
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
if TargetCarbon then
p = new Picture(w, h, 32)
else
p = new Picture(w, h)
End

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

End Function
[/code]

Yes I believe they are free. As to where to find them you would have to ask him as I can’t remember where I downloaded them from.

Thanks for the kind words :slight_smile: You can get it at this blog http://ezgl.tumblr.com where I’ll post all future updates.

At some point this has stopped working on Windows.
I have an application I built for a customer 2 years ago that used a very similar approach. It worked fine. I just recompiled it after a long time and it no longer works on Windows, but works on Mac. I replaced my code with the code above suspecting some nuance, but still no good.

Has anyone else encountered this?

I think I found the issue in a separate (old) thread
here

I think it’s the make MakeCurrent function that fixes it.