Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Woahbird · Oct 28, 2016 at 09:10 PM · scripting problemservercopy-pasteguiutility

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Woahbird · Oct 28, 2016 at 09:19 PM 0
Share

Just wanted to add that i imported .net dlls and used static .net methods to do it, it works.

avatar image ChadrickEvans Woahbird · Dec 22, 2016 at 09:58 AM 0
Share

Do you $$anonymous$$d sharing a sample of your script and what dlls are needed?

avatar image Shinho · Oct 03, 2017 at 10:28 PM -1
Share

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Shinho · Oct 04, 2017 at 10:12 PM 0
Share

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.

avatar image alelepd · Apr 26, 2018 at 03:57 AM 0
Share

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).

avatar image Hellium alelepd · Apr 26, 2018 at 07:01 AM 0
Share

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.

avatar image alelepd Hellium · Apr 28, 2018 at 12:59 AM 0
Share

Thanks. I imported the System.Windows.Forms in my project (as well as System.Drawing). Still no luck running the code.

avatar image
1

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());
         }
     }
 }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image naviln · Jan 26, 2019 at 01:23 AM 0
Share

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!

avatar image naviln naviln · Jan 26, 2019 at 01:43 AM 0
Share

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?

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alelepd · Apr 28, 2018 at 01:01 AM 0
Share

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.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help me understand SyncVars please 0 Answers

Unity networking tutorial? 6 Answers

can I use .rsp file to preserve static throughout edit mode and runtime? 0 Answers

How do I make an updatable newsletter? 0 Answers

Please Help me JAR Path Java HOME Preferences 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges