# Button to Email A Mimic

**URL:** <https://forum.inductiveautomation.com/t/button-to-email-a-mimic/3346>\
**Category:** Ignition\
**Tags:** vision\
**Created:** [September 27, 2011, 3:34pm UTC](https://forum.inductiveautomation.com/t/button-to-email-a-mimic/3346 "2011-09-27T15:34:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dodonnell999](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@Dodonnell999](https://forum.inductiveautomation.com/u/Dodonnell999)\
**Post date:** [September 27, 2011, 3:34pm UTC](https://forum.inductiveautomation.com/t/button-to-email-a-mimic/3346/1 "2011-09-27T15:34:51Z")

</div>

I have a request from a customer to provide a button to do a screen capture and email the screen capture to an email address entered by the user.  
I've done a button already to email pdf reports to an entered email address but I'm not sure if it would be possible to capture the screen as a jpg or bmp and email it.

Any ideas or advice would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [September 27, 2011, 4:17pm UTC](https://forum.inductiveautomation.com/t/button-to-email-a-mimic/3346/2 "2011-09-27T16:17:47Z")

</div>

Take a look at the [system.print.printToImage](http://inductiveautomation.com/support/usermanuals/ignition/index.html?system_print_printtoimage.htm) function.

---

<div class="post-metadata">

**Author:** ![f\_jons](https://avatars.discourse-cdn.com/v4/letter/f/9fc29f/32.png) [@f\_jons](https://forum.inductiveautomation.com/u/f_jons)\
**Post date:** [September 28, 2011, 12:22pm UTC](https://forum.inductiveautomation.com/t/button-to-email-a-mimic/3346/3 "2011-09-28T12:22:07Z")

</div>

I do this as a help function in my systems. Basically, when a user clicks the help button, it takes a screen shot then retargets into my help program.  
Here is a subsection of that code that would reside in the script modules.

```auto
def takeScreenShot(event):
	from system.gui import getParentWindow
	from system.print import createImage
	from system.net import sendEmail
	from java.io import ByteArrayOutputStream
	from javax.imageio import ImageIO
	# Get an image of the entire screen as seen by the user.
	# By using getParentWindow(event).parent, you are grabbing the entire session.
	# This will include any docked windows.
	# Is returned as a bufferedImage. Need to convert it to a byteArray.
	# Instantiate an object of ByteArrayOutputStream.
	baos = ByteArrayOutputStream(10000)
	# Write the bufferedImage returned by the getParentWindow(event).parent as a ByteArrayOutputStream.
	ImageIO.write(createImage(getParentWindow(event).parent), 'jpg', baos)
	# Flush the stream.
	baos.flush()
	# Convert the ByteArrayOutputStream to a byteArray.
	screenShot = baos.toByteArray()
	# Close the stream.
	baos.close()
	# Send the email. 
	sendEmail(smtp, from, subject, body, html, to, ['screenShot.jpg'], [screenShot])
      
```

Will have to replace the variables in the sendEmail call with your specific values.  
Could be converted to be used in an event handler as well.

---

<div class="post-metadata">

**Author:** ![Dodonnell999](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@Dodonnell999](https://forum.inductiveautomation.com/u/Dodonnell999)\
**Post date:** [September 28, 2011, 3:25pm UTC](https://forum.inductiveautomation.com/t/button-to-email-a-mimic/3346/4 "2011-09-28T15:25:18Z")

</div>

Many thanks for the code, it worked a treat!

---

<div class="post-metadata">

**Author:** ![Chad\_Campbell](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Chad\_Campbell](https://forum.inductiveautomation.com/u/Chad_Campbell)\
**Post date:** [July 26, 2023, 9:05pm UTC](https://forum.inductiveautomation.com/t/button-to-email-a-mimic/3346/5 "2023-07-26T21:05:32Z")

</div>

> [@f\_jons](#):
>
> ```auto
> def takeScreenShot(event):
> from system.gui import getParentWindow
> from system.print import createImage
> from system.net import sendEmail
> from java.io import ByteArrayOutputStream
> from javax.imageio import ImageIO
> # Get an image of the entire screen as seen by the user.
> # By using getParentWindow(event).parent, you are grabbing the entire session.
> # This will include any docked windows.
> # Is returned as a bufferedImage. Need to convert it to a byteArray.
> # Instantiate an object of ByteArrayOutputStream.
> baos = ByteArrayOutputStream(10000)
> # Write the bufferedImage returned by the getParentWindow(event).parent as a ByteArrayOutputStream.
> ImageIO.write(createImage(getParentWindow(event).parent), 'jpg', baos)
> # Flush the stream.
> baos.flush()
> # Convert the ByteArrayOutputStream to a byteArray.
> screenShot = baos.toByteArray()
> # Close the stream.
> baos.close()
> # Send the email. 
> sendEmail(smtp, from, subject, body, html, to, ['screenShot.jpg'], [screenShot])
> 
> ```

How might this be converted to an event handler?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [July 26, 2023, 11:09pm UTC](https://forum.inductiveautomation.com/t/button-to-email-a-mimic/3346/6 "2023-07-26T23:09:24Z")

</div>

Best is to **not**"convert it to an event handler". Just place that function in a project library script and **call it** from an event handler (passing it the event).

---

<div class="post-metadata">

**Author:** ![Charles\_Taylor\_II](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@Charles\_Taylor\_II](https://forum.inductiveautomation.com/u/Charles_Taylor_II)\
**Post date:** [January 26, 2024, 6:49pm UTC](https://forum.inductiveautomation.com/t/button-to-email-a-mimic/3346/7 "2024-01-26T18:49:58Z")

</div>

I am trying to use this code, but it seems I'm doing something wrong. My gateway logs tell me there is no module named gui... Am I missing something here? I created the code in the project library, called it from a button click in Perspective, but it doesn't like me, or I just don't know the magic words.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [January 26, 2024, 6:56pm UTC](https://forum.inductiveautomation.com/t/button-to-email-a-mimic/3346/8 "2024-01-26T18:56:05Z")

</div>

It will not work with Perspective, which is built on html5, not Java Swing like Vision.

See this thread [Perspective Screenshot Capture - Ignition - Inductive Automation Forum](https://forum.inductiveautomation.com/t/perspective-screenshot-capture/24312)
