Reflection .NET API
Walkthrough / Log User Input and Credit Card Access / Save Screens As Text Or Images
In This Topic
    Save Screens As Text Or Images
    In This Topic

    You can monitor and log the screens accessed by users during a session. This walkthrough shows how to save the screens as text and images.

    To save screens as text or images

    1. In Visual Studio, create a new Console Application project and add references for the following Reflection assemblies. (Depending on your version of Visual Studio, these can be found either on the .NET tab or under Assemblies | Extensions.)Attachmate.Reflection
      Attachmate.Reflection.Framework
      Attachmate.Reflection.Emulation.IbmHosts (if you're using IBM)
      Attachmate.Reflection.Emulation.OpenSystems (if you're using UNIX or OpenVMS)

    2. Replace all the code in the Program.cs file with the following code for the terminal you are using. This code gets a handle to a running instance of Reflection. If the type of terminal you want to monitor is selected in the workspace, it creates an event handler to save the screens as they are changed. 
      Check for terminal type and create event handler
      Copy Code
      using System;
      using System.Collections.Generic;
      using System.Text;
      using Attachmate.Reflection.UserInterface;
      using Attachmate.Reflection.Framework;
      using Attachmate.Reflection.Emulation.IbmHosts;
      using System.IO;
      namespace SaveScreens
      {
          class Program
          {                        
              static void Main(string[] args)
              {
                  //Get the last activated instance of Reflection
                  Application app = MyReflection.ActiveApplication;
      
                  //Make sure Reflection is running
                  if (app != null)
                  {
                      IFrame frame = (IFrame)app.GetObject("Frame");
                      IView view = (IView)frame.SelectedView;
      
                      //If this is an IBM terminal, save the screens
                      if (view.Control.ToString() == "Attachmate.Reflection.Emulation.IbmHosts.IbmTerminal")
                      {
                          IIbmTerminal terminal = (IIbmTerminal)view.Control;
                          IIbmScreen screen = terminal.Screen;
      
                          //Handle the BeforeSendControlKey event to save screens
                          screen.BeforeSendControlKey += new BeforeSendControlKeyEventHandler(screen_BeforeSendControlKey);
                      }
                  }
                  else
                  {
                      Console.WriteLine("Cannot find instance of Reflection");
                  }
                  Console.ReadKey();
              }
          }
      }
      
    3. Add the following screenNumber variable and event handler to the Program class. The screenNumber variable is used to provide unique names for screen images. The event handler gets all the text displayed on the screen and appends it to a text file. It also captures the screen and saves it as a .png file. 
     Alternatively, you could save screen images by turning on ScreenHistory and then saving all images before the session is closed.
    Save screens as text or images
    Copy Code
    //screenNumber is a counter used to provide a unique name for each screen image file
    static public int screenNumber = 0;
    
    //Handle BeforeSendControlKey to save the screen before the Transmit key is sent
    static void screen_BeforeSendControlKey(object sender, BeforeSendControlKeyEventArgs args)
    {
        IIbmScreen screen = (IIbmScreen)sender;
        byte[] screenImage;
    
        //Write output only if the control key is Transmit. (Don't write output if tabs or other
        //control keys are pressed.)
        if (args.Key.Equals(ControlKeyCode.Transmit) == true)
        {
            screenNumber++;
    
            //Get all the text on the screen and append it to a file along with a divider to indicate the end of the screen
            string text = screen.GetTextEx(1, 1, screen.Rows, screen.Columns, GetTextArea.Block, GetTextWrap.Off, GetTextAttr.Any, GetTextFlags.CRLF);
            string fileName = @"C:\users\" + Environment.UserName + @"\Documents\userInputLog.txt";
            StreamWriter w = File.AppendText(fileName);
            w.WriteLine(text);
            w.WriteLine("...................................................................");
            w.Close();
    
            //Save screens as images.
            screenImage = screen.Parent.Productivity.ScreenHistory.GetLiveScreenImage();
            string imageFileName = @"C:\users\" + Environment.UserName + @"\Documents\Screen" + screenNumber.ToString() + @".png";
            File.WriteAllBytes(imageFileName, screenImage);
        }
    }
    

    To test this project

    1. Open Reflection and then open a session document file.
    2. In Visual Studio, press F5 to run the project.
    3. Enter some data and navigate to a few different screens.
    4. Open the userInputLog.txt log file in your Documents folder and verify that the data you entered was logged in the file.
    5. Verify that the screen images are saved in your Documents folder as numbered .png files.