Saving window content as an image

In this example we will show you how to save window content to the graphic file and how to implement this functionality in your Cinder application. This could be useful to save output of a graphics algorithm.

How to do it…

We will add a window content saving function to your application:

  1. Add necessary headers:
    #include "cinder/ImageIo.h"
    #include "cinder/Utilities.h"
  2. Add property to your application's main class:
    bool mMakeScreenshot;
  3. Set a default value inside the setup method:
    mMakeScreenshot = false;
  4. Implement the keyDown method as follows:
    void MainApp::keyDown(KeyEvent event)
      {
      if(event.getChar() == 's') {
      mMakeScreenshot = true;
        }
      }
  5. Add the following code at the end of the draw method:
    if(mMakeScreenshot) {
    mMakeScreenshot = false;
    writeImage( getDocumentsDirectory() / fs::path("MainApp_screenshot.png"), copyWindowSurface() );
    }

How it works…

Every time you set mMakeScreenshot to true the screenshot of your application will be selected and saved. In this case the application waits for the S key to be pressed and then sets the flag mMakeScreenshot to true. The current application window screenshot will be saved inside your documents directory under the name MainApp_screenshot.png.

There's more...

This is just the basic example of common usage of the writeImage function. There are many other practical applications.

Saving window animation as image sequences

Let's say you want to record a equence of images Perform the following steps to do so:

  1. Modify the previous code snippet shown in step 5 for saving the window content as follows:
    if(mMakeScreenshot || mRecordFrames) {
    mMakeScreenshot = false;
    writeImage( getDocumentsDirectory() / fs::path("MainApp_screenshot_" + toString(mFramesCounter) + ".png"), copyWindowSurface() );
    mFramesCounter++;
    }
  2. You have to define mRecordFrames and mFrameCounter as properties of your main application class:
    bool mRecordFrames;
    int mFramesCounter;
  3. Set initial values inside the setup method:
    mRecordFrames = false;
    mFramesCounter = 1;

Recording sound visualization

We assume that you are using TrackRef from the audio namespace to play your sound Perform the following steps:

  1. Implement the previous steps for saving window animations as image sequences.
  2. Type the following lines of code at the beginning of the update method:
    if(mRecordFrames) {
    mTrack->setTime(mFramesCounter / 30.f);
    }

We are calculating the desired audio track position based on the number of frames that passed. We are doing that to synchronize animation with the music track. In this case we want to produce 30 fps animation so we are dividing mFramesCounter by 30.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.255.86