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

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.

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.

It is an OpenGL project.

But do you have an OpenGL related question?

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().

SOIL” (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
	);