Example Code

Programmatically Acquire Screenshot in Executable Application

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Description

This example uses calls from the Windows user32.dll to programmatically take a screenshot, access the image stored in the clipboard, and save the image to a file. This method can be used in a deployed application whereas innate LabVIEW methods (Clipboard.Get Image) as not available at run-time and cannot be used in an executable.

 

How to Use

Steps to Implement or Execute Code

  1. Use snippet below or download the attached LabVIEW code.
    • Provided code is compatible with LabVIEW 2012 and later.
  2. Run VI

**Note that the VI must be set to run in a single thread (run in UI thread).**

 


Block Diagram.png

James K.
National Instruments
Applications Engineer

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
h_yong
Active Participant
Active Participant
on

Hi, I'm trying to run this on LV2011F2 and get this error when tryign to save:

Error calling method System.Drawing.Image.Save, (System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.

    Inner Exception: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.) <append><b>System.Runtime.InteropServices.ExternalException</b> in PrintScreen-LV86-4.vi

Any ideas? This happens with both the VI and a compiled executable. I do have the execution set to UI. Thanks.

FireFist-Redhawk
Active Participant
Active Participant
on

For anyone who experienced it, in order for this to work in runtime environment, you have to set up the Call Library function node the way that this thread shows:

 

https://forums.ni.com/t5/LabVIEW/application-crash-report-user32-dll/td-p/2414136?profile.language=e...

Redhawk
Test Engineer at Moog Inc.

Saying "Thanks that fixed it" or "Thanks that answers my question" and not giving a Kudo or Marked Solution, is like telling your waiter they did a great job and not leaving a tip. Please, tip your waiters.