How to copy an image to the system clipboard?

Hello, i am looking for a way to copy an image to the system clipboard in unity. I know that i can copy text with GUIUtility.systemCopyBuffer, but it only accepts strings. Is there a way to do this in unity?

Try this to set the image to clipboard:

   Clipboard.SetImage(replacementImage);

And this to retreive the image from clipboard:

    returnImage = Clipboard.GetImage();

In order to complete @neosca 's answer, here is a complete step-by-step solution:

1. In your project, create a Plugins folder

2. Copy in this folder the following Mono dll:

- <path_to_Unity>\Editor\Data\Mono\lib\mono\2.0\System.Drawing.dll

- <path_to_Unity>\Editor\Data\Mono\lib\mono\2.0\System.Windows.Forms.dll

3. In your code, add the following function.

private void CopyToClipboard( Texture2D texture )
{
    System.IO.Stream s = new System.IO.MemoryStream(texture.width * texture.height);
    byte[] bits = texture.EncodeToJPG();
    s.Write(bits, 0, bits.Length);
    System.Drawing.Image image = System.Drawing.Image.FromStream(s);
    System.Windows.Forms.Clipboard.SetImage(image);
    s.Close();
    s.Dispose();
}

4. If you want to test easily this function:

1. Create a new RenderTexture in your project (Create > Render texture)

2. Create a new camera in your scene, remove its audio listener, and provide the RenderTexture in the RenderTarget field

3. Create a new script called CopyTextureToClipboard and attach it to your camera / an empty, without forgetting to provide the same RenderTexture as the camera’s, and press F12 to copy a screenshot into your clipboard:

using System.Collections;
using UnityEngine;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

public class CopyTextureToClipboard : MonoBehaviour {

    public RenderTexture rendertexture;
    
    private Texture2D CaptureScreenshot()
    {
        Texture2D texture = new Texture2D(rendertexture.width, rendertexture.height);
        RenderTexture.active = rendertexture;
        texture.ReadPixels(new Rect(0, 0, rendertexture.width, rendertexture.height), 0, 0);
        texture.Apply();
        RenderTexture.active = null;

        return texture;
    }

    private void CopyToClipboard( Texture2D texture )
    {
        Stream s = new MemoryStream(texture.width * texture.height);
        byte[] bits = texture.EncodeToJPG();
        s.Write(bits, 0, bits.Length);
        Image image = Image.FromStream(s);
        Clipboard.SetImage(image);
        s.Close();
        s.Dispose();
    }

    // Update is called once per frame
    void Update ()
    {
        if( Input.GetKeyDown( KeyCode.F12 ))
        {
            CopyToClipboard(CaptureScreenshot());
        }
    }
}

I also in need of knowing how to do it, been search in every place with no success

Thanks for your answer @neosca . I have tried using that approach, but I was checking for Clipboard.ContainsImage() which broke every clipboard behavior on the system. Your comment made me test for getting the image without testing if the clipboard contained an image, which result in some data, now i’m ready for the next step converting the bitmap to the texture2d. Many thanks.

There’s some relevant info here.
https://stackoverflow.com/questions/46576473/c-sharp-clipboard-getimage-from-photoshop-region-returns-a-b-w-image
I was able to copy the image but still has some artifacts, and its hard to retrieve which type of image is coming in data.

Thanks to @neosca 's answer, I know how to achieve the sysem’s clipboard.

And I have another solution to make my code with System.Windows.Forms.Clipboard and System.Drawing.Image pass the compilation, without copying any dll plugins, that is, use a csc.rsp file in the Unity Project:

  1. Find the location of .asmdef file

  2. In that directory, create a csc.rsp file, and edit it with notepad or something else

  3. Copy following lines to it.

-r:System.Windows.Forms.dll

-r:System.Drawing.dll

  1. Back to Unity, refresh and recompile