PDA

View Full Version : [Need Help] About saving the current screen as an image file (.png, .jpg)



x1363x
10-28-2016, 02:22 PM
Hello,
I have recently started to build a paint-like drawing program. All of the features except "save" and "open(a saved drawing)" working as intended. I couldn't find a way adding these two.
I am coding the program with CodeBlocks IDE and as a "C" project(.c). Any suggestions?
All answers are appreciated. Thank you.

Dark Photon
10-28-2016, 05:05 PM
This is an OpenGL forum, and your post doesn't appear to have anything to do with OpenGL. Check out the Code::Blocks documentation or a related discussion forum.

x1363x
10-29-2016, 01:18 PM
It is an OpenGL project.

Cornix
10-29-2016, 01:20 PM
But do you have an OpenGL related question?

GClements
10-29-2016, 01:33 PM
An "paint" program which uses OpenGL for painting should probably be using framebuffer objects (FBOs) so that you're guaranteed to have a copy of the painted image. Rendering directly to a window isn't much use, as there are no guarantees that the window contents will be preserved. Portions of the window which are obscured by other windows (e.g. dialogs which appear at inconvenient moments) aren't guaranteed to be retained.

For this reason, painting directly to a window isn't a good idea regardless of which rendering API you're using.

You can read the contents of any framebuffer (either a FBO or the default framebuffer) with glReadPixels(). In older versions of OpenGL, you could render pixel data directly to a framebuffer with glDrawPixels(), but that function isn't present in modern versions. Instead, you have to upload the pixel data to a texture first. Then you can render primitives using that texture, or bind it to a framebuffer object and use glBlitFramebuffer().

john_connor
10-29-2016, 02:45 PM
"SOIL (http://www.lonesock.net/soil.html)" (a C library for images) has a function to do that:



/* save a screenshot of your awesome OpenGL game engine, running at 1024x768 */
int save_result = SOIL_save_screenshot
(
"awesomenessity.bmp",
SOIL_SAVE_TYPE_BMP,
0, 0, 1024, 768
);