Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 /
  • Help Room /
avatar image
1
Question by Shinho · Oct 03, 2017 at 10:32 PM · importing problems

Clipboard image import

Is it possible to import an image from the clipboard to a texture2D? Seems system buffer can only recognize strings...

Comment
Add comment · Show 1
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 26, 2018 at 03:50 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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




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 naviln · Jan 07, 2019 at 03:31 PM 0
Share

Amazing, thankyou!

avatar image
0

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.

alt text

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.

alt text

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



screenshot-2021-11-02-110815.png (69.9 kB)
screenshot-2021-11-02-111505.png (86.4 kB)
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

119 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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