官术网_书友最值得收藏!

Screen grabbing

Sometimes it is desirable to save the picture drawn by your project in the file. You can do it using tools of your operating system, but it's more comfortable to do it right in your project. So let's see how to save the contents of your project screen to an image file.

For such purposes, we need to use the ofImage class for working with images. Though the class is considered in Chapter 4, Images and Textures, for screen grabbing, it is just enough to understand that the ofImage object holds an image.

The following code saves the current screen to file on the pressing of the Space bar. It should be added to the testApp::keyPressed() function as follows:

//Grab the screen image to file
if ( key == ' ' ) {
  ofImage image;  //Declare image object

  //Grab contents of the screen
  image.grabScreen( 0, 0, ofGetWidth(), ofGetHeight() );  

  image.saveImage( "screen.png" );  //Save image to file
}

The parameters of the image.grabScreen() function specify the rectangle of the grabbing. In our case, it is the whole screen of the project.

This code is implemented in the 02-2D/06-Spirals example. Run it and press the Space bar; the contents of the screen will be saved to the bin/data/screen.png file in your project's folder.

Tip

The PNG files are small and have high quality, so we often use these for screen grabbing. But, writing to a PNG file takes some time because the image has to be compressed. It takes up to several seconds, depending on the CPU and image size. So if you need to save images fast, use the BMP file format.

image.saveImage( "screen.bmp" );
主站蜘蛛池模板: 洪泽县| 淅川县| 吉首市| 南郑县| 隆安县| 金乡县| 尼木县| 马山县| 元氏县| 湖南省| 乌海市| 水富县| 广灵县| 岳池县| 安庆市| 五指山市| 北票市| 榆中县| 巴彦淖尔市| 班戈县| 敖汉旗| 六枝特区| 嫩江县| 彭山县| 同仁县| 永吉县| 大洼县| 瑞安市| 寻乌县| 化州市| 天全县| 定边县| 独山县| 镇远县| 会泽县| 咸宁市| 同江市| 永年县| 嘉峪关市| 河曲县| 万源市|