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

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" );
主站蜘蛛池模板: 封开县| 宾川县| 永昌县| 乳源| 房产| 武义县| 锡林郭勒盟| 金山区| 巢湖市| 丁青县| 长乐市| 阳朔县| 虎林市| 贵南县| 招远市| 神农架林区| 东阿县| 武功县| 庆元县| 黄浦区| 珠海市| 江永县| 巧家县| 吐鲁番市| 锡林浩特市| 莱阳市| 绍兴市| 禹城市| 德安县| 宁德市| 南靖县| 蛟河市| 沙河市| 互助| 阿坝县| 洪湖市| 根河市| 宜昌市| 嘉义市| 梧州市| 芷江|