- Home /
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?
Just wanted to add that i imported .net dlls and used static .net methods to do it, it works.
Do you $$anonymous$$d sharing a sample of your script and what dlls are needed?
I also in need of knowing how to do it, been search in every place with no success
Answer by neosca · Oct 04, 2017 at 06:46 AM
Try this to set the image to clipboard:
Clipboard.SetImage(replacementImage);
https://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.setimage(v=vs.110).aspx
And this to retreive the image from clipboard:
returnImage = Clipboard.GetImage();
https://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.getimage(v=vs.110).aspx
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. $$anonymous$$any thanks.
Humm I wonder how do you make it work, unity doesn't have any Clipboard.GetImage(); support. Or maybe am I missing something?
It would be great if you could explain how is this working. Also, does this work on $$anonymous$$ac? (I guess no, but I am just curious).
As indicated in the documentation page of Clipboard.SetImage
, this function lies in the System.Windows.Forms
namespace.
Thus, you will have to import it in your script, and add the appropriate dll in your Plugins
folder. You can find the $$anonymous$$ono dll used by Unity in the following folder:
<path_to_Unity>\Editor\Data\$$anonymous$$ono\lib\mono\2.0
I don't know if you will be able to run it on $$anonymous$$ac.
Thanks. I imported the System.Windows.Forms in my project (as well as System.Drawing). Still no luck running the code.
Answer by Hellium · Apr 28, 2018 at 07:28 AM
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());
}
}
}
This is excellent, thank you. I managed to use this and get copy paste functionality working in my app from inside the Unity Editor.
Unfortunately though, when I compile a build, and try to run the System.Windows.Forms.Clipboard.GetImage() method, I get an access violation error.
Read from location FFFFFFFFAA865DB8 caused an access violation.
Stack Trace of Crashed Thread 9304:
0x00007FFC667B42E0 (mono) mono_unity_install_unitytls_interface
ERROR: SymGetSymFromAddr64, GetLastError: 'The specified module could not be found.' (Address: 00000228A47BC127)
ERROR: SymGet$$anonymous$$oduleInfo64, GetLastError: 'A dynamic link library (DLL) initialization routine failed.' (Address: 00000228A47BC127)
The crash dump has:
The thread tried to read from or write to a virtual address for which it does not have the appropriate access.
Does anyone know how to resolve this? I tried right click>running as ad$$anonymous$$ but it hasn't solved the issue.
Cheers!
O$$anonymous$$, so I go this working - I switched my Editor to produce a 32 bit build (ins$$anonymous$$d of a 64 bit one), and it worked fine. (I guess the System.Windows.Forms dll i copied from the Unity installation directory are 32 bit ones.)
Does anyone know if/where I can get 64 bit copies of them?
Answer by Shinho · Apr 26, 2018 at 07:54 AM
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.
I get this error anytime I try to do something (like running your code example) that uses System.Windows.Form: "EntryPointNotFoundException: InstallTrackingHandler System.Windows.Forms.CarbonInternal.Dnd..cctor ()"
I tried by changing the API Compatibility to "Net 2.0" (ins$$anonymous$$d of Net 2.0 Subset) but the code doesn't work either. I am not sure if this will ever work on $$anonymous$$ac.
Answer by dbdenny · May 26 at 08:34 AM
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
4) Back to Unity, refresh and recompile
Your answer
![](https://koobas.hobune.stream/wayback/20220612095618im_/https://answers.unity.com/themes/thub/images/avi.jpg)