Saving GL data to file


I’m trying to save GL data to an image file.

I have this:

	public void SaveUno() {
		int size = (int) ActualSize[0] * (int) ActualSize[1] * 4;
		var data = new byte[size];
		int w = (int) ActualSize[0];
		int h = (int) ActualSize[1];
		OpenGL.GL.ReadPixels(0,0, w, h, GLPixelFormat.Rgba, GLPixelType.UnsignedByte, data);
		SaveImage(data, size, w, h, "/Users/bolav/Image.png");

	public void SaveImage(byte[] data, int size, int w, int h, string fn)
		uBase::Buffer *buf = uBase::Buffer::CopyFrom(data, size);

		uImage::Bitmap *bmp = new uImage::Bitmap(w, h, uImage::FormatRGBA_8_8_8_8_Int, buf, 0, 0);
		uBase::String tfn = "/Users/bolav/Image.png";
		uImage::Png::Save(tfn, bmp);

But I have some issues.

The data array does not seem to get the actual data, so I’m doing something wrong there.

How can I convert from uString to uBase::String?

The Png save crashes, so I’m certain that there are some errors with creating the bitmap or something around there.

ActualSize is in points, not pixels… that could be the issue?

Open.GL.ReadPixels returns 255 for all bytes, so I think I have to attach it or something to get actual data. Also uImage::Png::Save just crashes.

What element are you trying to read data from?

MapViews are known to not work, becuase of a DRM mechanism in the OS.

Also, what framebuffer have you attached?

Png::Save will crash with your above code because you are interpreting points as pixels (i.e. your buffer is too small)

You might also want to do GL.PixelStore(GLPixelStoreParameter.PackAlignment, 1); to avoid OpenGL computing padding at the end of each scanline.

I’ve created a Control that you can draw on. I’m trying to dump it to an image. I’ve not connected any framebuffer, the whole save code is what you see. I understand that this is the reason it’s not working, and was hoping for a full example.

On CMake the error from Png::Save is Unsupported PNG Format: FormatRGBA_8_8_8_8_Int. It might be the same error on the other platforms.


I’ve been trying to save two pictures that get imported from camera roll as one PNG. So i have a canvas (DockPanel) with two movable pictures in it. These need to be saved together as one PNG.

So, how can i use the framebuffer class to generate a PNG file for me? Have you figured this out yet Bjørn-Olav?

Thanks so much!

@bolav: The Png::Save issue is because we require FormatRGBA_8_8_8_8_UInt_Normalize instead of FormatRGBA_8_8_8_8_Int in the png-saver.

Erik helped me solve this, and created these great components!

Great components!! I’m impressed by that Uno level.
A month ago we had to go native because Fuse did not have a component like this