Exporting to Image Files

Once you've drawn to a graphics resource, it needs to be output to either the display or a file in order to be useful. This section explains how to export the graphics to an image file.

- Table of Contents -

Exporting a Graphics Resource to an Image File

In addition to displaying a graphics resource on the screen, you can also export it to an image file using the "exportGraphics" function from the Graphics library. Supported formats include BMP, PNG, and JPEG.

- Function Format -
void exportGraphics( int graphicsID, string fileName, string format )

Arguments:

If you're exporting to JPEG format, you can optionally specify image quality by adding an additional argument at the end:

- Function Format -
void exportGraphics(
  int graphicsID, string fileName, string format, float quality
)

The "quality" parameter can be set in the range from 0.0 (lowest quality) to 1.0 (highest quality). You can also specify a value up to 100.0, in which case it will be interpreted as a percentage.

Example Program

Let's try generating a window and exporting the graphics resource. Write and run the following program:


import Graphics;
import Graphics2D;
import GUI;

// Create a graphics resource
int graphicsID = newGraphics( );

// Create a renderer
int rendererID = newGraphics2DRenderer( 800, 600, graphicsID );

// Export the graphics resource to a PNG file
exportGraphics( graphicsID, "Test.png", "PNG" );
Sample.vcssl

This program won't show anything on the screen, so after launching it, wait a few seconds and then close the console window.

You should find a file named Test.png in the same folder as the program. Since we didn't draw anything, the image will be completely blank (transparent).