Clipboard image import
Is it possible to import an image from the clipboard to a texture2D? Seems system buffer can only recognize strings...
Any progress on this? I am also needing this feature (for $$anonymous$$ac and Windows standalone). So far, it is just text, but no image content is available.
Answer by Hellium · Apr 28, 2018 at 07:48 AM
Here is a solution that I've tested and works fine on Windows:
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 Texture2D RetrieveFromClipboard()
{
System.Drawing.Image image = System.Windows.Forms.Clipboard.GetImage();
System.IO.MemoryStream s = new System.IO.MemoryStream(image.Width * image.Height);
image.Save(s, System.Drawing.Imaging.ImageFormat.);
Texture2D texture = new Texture2D( image.Width, image.Height );
texture.LoadImage(s.ToArray());
s.Close();
s.Dispose();
return texture;
}
4. If you want to test easily this function:
1. Create a new UI Image
2. Go to the following address to get a random image : http://lorempixel.com/400/200/ and copy the image using Right click ► Copy image
3. Create a new script called CopyTextureToClipboard and attach it to your camera / an empty. Drag & drop the image into the image field of the component, run the game and press F10 to see the image you have copied into your UI:
using UnityEngine;
public class RetrieveTextureFromClipboard : MonoBehaviour {
public UnityEngine.UI.Image image;
private Texture2D RetrieveFromClipboard()
{
System.Drawing.Image image = System.Windows.Forms.Clipboard.GetImage();
System.IO.MemoryStream s = new System.IO.MemoryStream(image.Width * image.Height);
image.Save(s, System.Drawing.Imaging.ImageFormat.Jpeg);
Texture2D texture = new Texture2D( image.Width, image.Height );
texture.LoadImage(s.ToArray());
s.Close();
s.Dispose();
return texture;
}
// Update is called once per frame
void Update ()
{
if( Input.GetKeyDown( KeyCode.F10 ))
{
Texture2D texture = RetrieveFromClipboard();
image.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one * 0.5f);
image.preserveAspect = true;
}
}
}
Answer by stuckwiththisname · Nov 02, 2021 at 03:42 PM
The Problem
@Hellium 's answer is very helpful, but it doesn't work in Unity 2019.4.19f. Based on other posts online, it seems to have been broken in the first release of 2019.
The problem is that the implementation of System.Windows.Forms.Clipboard is now a nulled-out mock object. All Set methods are empty and all Get methods throw NullReferenceException.

Selecting the right assembly
On my computer, there are many folders for .NET versions in *...\Unity\Hub\Editor\2019.4.30f1\Editor\Data\MonoBleedingEdge\lib\mono*. This is the folder that I'm using. Grab both assemblies from this folder.
I reflected many of these files and the one in ...\Unity\Hub\Editor\2019.4.30f1\Editor\Data\MonoBleedingEdge\lib\mono\4.5 has an implementation.

It looks like the file in unityjit and the gac folder probably have an implementation too, based on their file size. I'd avoid these two files unless you need them.
DO NOT use the file in any of the "api" folder, such as 2.0-api, 4.0-api, or 4.5.1-api. All of the "api" folders are have an empty mock class for Clipboard.
Current problem
The next problem that you'll run into is that Unity will crash when you try to call GetImage(). Calling ContainsImage() will function correctly, but when you try to get the image then Unity will crash. Wrap it in a try/catch block, try it in the immediate window... as soon as GetImage() is invoked, Unity goes down.
Here's the useful part of the stack. It gets so close, but it fails during Marshal.Copy, which is used to copy data from an unmanaged memory pointer to a managed 32-bit signed integer array. At that point, it jumps into external unmanaged code, in marshal.c and memcpy.asm, and the user base is probably not finding a work around to a problem that goes this deep.
0x00007FFBACA45537 (mono-2.0-bdwgc) [f:\dd\vctools\crt\vcruntime\src\string\amd64*memcpy.asm:135] memcpy 0x00007FFBAC7BC351 (mono-2.0-bdwgc) [c:\users\builduser\builds\tsuywz8z\0\vm\mono\mono\metadata*marshal.c:11069] ves_icall_System_Runtime_InteropServices_Marshal_copy_from_unmanaged 0x0000019792E67154 (mscorlib) System.Runtime.InteropServices.Marshal.copy_from_unmanaged() 0x0000019792DE9C4B (mscorlib) System.Runtime.InteropServices.Marshal.Copy() 0x0000019792DE3B4B (System.Windows.Forms) System.Windows.Forms.XplatUIWin32.DIBtoImage() 0x0000019792DE0613 (System.Windows.Forms) System.Windows.Forms.XplatUIWin32.ClipboardRetrieve() 0x0000019792DDF9BA (System.Windows.Forms) System.Windows.Forms.XplatUI.ClipboardRetrieve() 0x0000019792DD596B (System.Windows.Forms) System.Windows.Forms.Clipboard.GetDataObject() 0x0000019792DD55BB (System.Windows.Forms) System.Windows.Forms.Clipboard.GetDataObject() 0x0000019792DD534B (System.Windows.Forms) System.Windows.Forms.Clipboard.GetImage() 0x0000019792DD46B3 (Assembly-CSharp) RetrieveTextureFromClipboard.RetrieveFromClipboard()
Your answer
Follow this Question
Related Questions
FBX prefabs fail to reimport 6 Answers
Unity third person not running smoothly. 0 Answers
Problem importing model into Unity 0 Answers
Animation import: What am i doing wrong? 0 Answers
cannot import package in play mode 2 Answers