How To - Practical Examples > How To ... Develop Tools Using DotNet >

# How To ... Copy The Last Rendered Image To The Windows Clipboard

How To > Use DotNet > Copy The Last Rendered Image To The Windows Clipboard

The following tutorial demonstrates the use of the System.Windows.Forms.Clipboard DotNetClass to read and write bitmaps from and to the Windows Clipboard.

The script can be saved into one of the Startup folders to be evaluated on startup. The custom function will be executed by a PostRender callback each time the renderer finishes, putting the last rendered image into the Windows Clipboard automatically.

Related topics:

DotNet In MAXScript

dotNetClass

dotNetClass:System.Windows.Forms.Clipboard

NATURAL LANGUAGE
Define a global function to be called by a general callback after each rendering.
Get the DotNet class exposing Windows Clipboard functionality
Get the last rendered image via MAXScript
Save the image to disk
Convert the image to a DotNet Image
Put the image into the Windows Clipboard
Release any memory related to the image and delete the file from disk
Returns true if the clipboard now contains an image
Unregister any callbacks with the same ID
Register a PostRender callback with a custom ID that calls the function

SCRIPT:
    fn CopyRenderToClipboard =
    (
      clipboardClass = dotNetClass "System.Windows.Forms.Clipboard"
      b = getLastRenderedImage()
      theFileName = GetDir #image +"\\_renderToClipboard.bmp"
      b.filename = theFileName
      save b
      close b
      theImage = dotNetClass "System.Drawing.Image"
      theBitmap = theImage.FromFile theFileName
      clipboardClass.setImage theBitmap
      theBitmap.Dispose()
      deleteFile theFileName
      clipboardClass.ContainsImage()
    )--end fn

    callbacks.removeScripts id:#CopyRenderToClipboard
    callbacks.addScript #PostRender "CopyRenderToClipboard()" id:#CopyRenderToClipboard

## Step-By-Step

    fn CopyRenderToClipboard = (

Define a global function. We will place the script in a startup folder, so we want the function to be visible in the global scope.

    clipboardClass = dotNetClass "System.Windows.Forms.Clipboard"

Get the Clipboard dotNetClass from System.Windows.Forms

    b = getLastRenderedImage()

Get the last rendered image

    theFileName = GetDir #image + "\\_renderToClipboard.bmp"

Define a temporary file name.

    b.filename = theFileName

Set the bitmap's file name to the one defined in the previous step.

    save b

Save the bitmap to disk under the assigned file name.

    close b

Close the bitmap.

    theImage = dotNetClass "System.Drawing.Image"

Get the Image dotNetClass from System.Drawing

    theBitmap = theImage.FromFile theFileName

Get the saved image from file as bitmap

    clipboardClass.setImage theBitmap

Copy the image to the clipboard by calling the setImage method exposed by the clipboard class.

    theBitmap.Dispose()

Release the bitmap - it is already in the Windows Clipboard!

    deleteFile theFileName

Delete the temporary file from disk.

    clipboardClass.ContainsImage()

Return true if the Windows Clipboard contains an image, which we can assume is our image.

    )--end fn

This is the end of the global function.

Now we will register a general callback to call the function whenever the renderer finishes.

    callbacks.removeScripts id:#CopyRenderToClipboard

First we make sure we remove any callbacks with the same ID as the one we are about to register. This way, you can run the script multiple times (for example while further developing the function or making changes to the code in general).

    callbacks.addScript #PostRender "CopyRenderToClipboard()" id:#CopyRenderToClipboard

Then we register a PostRender callback to run the function whenever the renderer finishes.

#### See Also

- "How To" Tutorials Index Page