Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Screenshot and copy to clipboard

Discussion in 'Editor & General Support' started by G4den, Mar 19, 2019.

  1. G4den

    G4den

    Joined:
    Oct 30, 2016
    Posts:
    7
    Hi Unity3D Community

    I got this screenshot and copy clipboard code.

    I works super nice in in the editor. But in the standalone i get an error.

    I've made countless google searches on the problem but i couldn't find any solution


    It works fine without the copy to clipboard section.

    i've copied the System.Drawing.dll and System.Windows.Forms into the asset Plugins folder



    Hope you guys can help me. Thanks! :D



    The code:
    Code (CSharp):
    1.  
    2.    private IEnumerator takeScreenShot()
    3.    {
    4.        yield return new WaitForEndOfFrame(); // it must be a coroutine
    5.  
    6.  
    7.        yield return new WaitForEndOfFrame();
    8.  
    9.        RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
    10.        cam.targetTexture = rt;
    11.        var _screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGBA32, false);
    12.        cam.Render();
    13.        RenderTexture.active = rt;
    14.        _screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
    15.        _screenShot.Apply();
    16.  
    17.        cam.targetTexture = null;
    18.        RenderTexture.active = null;
    19.        Destroy(rt);
    20.        // split the process up--ReadPixels() and the GetPixels() call inside of the encoder are both pretty heavy
    21.        yield return 0;
    22.  
    23.        var bytes = _screenShot.EncodeToPNG();
    24.  
    25.        Destroy(_screenShot);
    26.  
    27.        //Writing bytes to a file
    28.        File.WriteAllBytes(UnityEngine.Application.dataPath + "/Images/Header.png", bytes);
    29.  
    30.        Image img;
    31.        using (var ms = new MemoryStream(bytes))
    32.        img = Image.FromStream(ms);
    33.        Clipboard.SetImage(img);
    34.  
    35.        Debug.Log("Picture taken");
    36.    }


    The error log when i use it in standalone
    Code (CSharp):
    1.  
    2. Mono path[0] = 'C:/Users/Emil/Desktop/Discord Text Generator/Text Generator_Data/Managed'
    3. Mono config path = 'C:/Users/Emil/Desktop/Discord Text Generator/MonoBleedingEdge/etc'
    4. PlayerConnection initialized from C:/Users/Emil/Desktop/Discord Text Generator/Text Generator_Data (debug = 0)
    5. PlayerConnection initialized network socket : censored
    6. Multi-casting "[IP] censored[Port] censored[Flags] 2 [Guid] censored[EditorId] censored[Version] 1048832 [Id] WindowsPlayer(DESKTOP-VR5NF2J) [Debug] 0 [PackageName] WindowsPlayer" to [censored:censored]...
    7. Started listening to [censored:censored]
    8. PlayerConnection already initialized - listening to [censored:censored]
    9. Initialize engine version: 2019.2.0a8 (18a4512f903f)
    10. [XR] Discovering subsystems at path C:/Users/Emil/Desktop/Discord Text Generator/Text Generator_Data/UnitySubsystems
    11. GfxDevice: creating device client; threaded=1
    12. Direct3D:
    13.    Version:  Direct3D 11.0 [level 11.1]
    14.    Renderer: Radeon (TM) RX 480 Graphics (ID=0x67df)
    15.    Vendor:  
    16.    VRAM:     8172 MB
    17.    Driver:   25.20.15021.1007
    18. Begin MonoManager ReloadAssembly
    19. - Completed reload, in  0.088 seconds
    20. <RI> Initializing input.
    21.  
    22. <RI> Input initialized.
    23.  
    24. <RI> Initialized touch support.
    25.  
    26. UnloadTime: 0.459700 ms
    27. Fallback handler could not load library C:/Users/Emil/Desktop/Discord Text Generator/Text Generator_Data/Mono/libc
    28. Fallback handler could not load library C:/Users/Emil/Desktop/Discord Text Generator/Text Generator_Data/Mono/libc.dll
    29. Fallback handler could not load library C:/Users/Emil/Desktop/Discord Text Generator/Text Generator_Data/Mono/libc
    30. NullReferenceException: Object reference not set to an instance of an object
    31.   at System.Drawing.ComIStreamMarshaler+ManagedToNativeWrapper..cctor () [0x00049] in <2853fe23daea44619252c26e80ec312d>:0
    32. Rethrow as TypeInitializationException: The type initializer for 'ManagedToNativeWrapper' threw an exception.
    33.   at System.Drawing.ComIStreamMarshaler.MarshalManagedToNative (System.Object managedObj) [0x00000] in <2853fe23daea44619252c26e80ec312d>:0
    34.   at (wrapper managed-to-native) System.Drawing.GDIPlus.GdipLoadImageFromStream(System.Runtime.InteropServices.ComTypes.IStream,intptr&)
    35.   at System.Drawing.Image.InitFromStream (System.IO.Stream stream) [0x000c7] in <2853fe23daea44619252c26e80ec312d>:0
    36.   at System.Drawing.Image.LoadFromStream (System.IO.Stream stream, System.Boolean keepAlive) [0x00011] in <2853fe23daea44619252c26e80ec312d>:0
    37.   at System.Drawing.Image.FromStream (System.IO.Stream stream) [0x00000] in <2853fe23daea44619252c26e80ec312d>:0
    38.   at ScreenShotRect+<takeScreenShot>d__15.MoveNext () [0x001af] in E:\Never delete\Unity projekter\Text Generator\Assets\ScreenShotRect.cs:76
    39.   at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00028] in C:\buildslave\unity\build\Runtime\Export\Scripting\Coroutines.cs:17
    40.  
    41. (Filename: <2853fe23daea44619252c26e80ec312d> Line: 0)
    42.  
    43. NullReferenceException: Object reference not set to an instance of an object
    44.   at System.Drawing.ComIStreamMarshaler+ManagedToNativeWrapper..cctor () [0x00049] in <2853fe23daea44619252c26e80ec312d>:0
    45. Rethrow as TypeInitializationException: The type initializer for 'ManagedToNativeWrapper' threw an exception.
    46.   at System.Drawing.ComIStreamMarshaler.MarshalManagedToNative (System.Object managedObj) [0x00000] in <2853fe23daea44619252c26e80ec312d>:0
    47.   at (wrapper managed-to-native) System.Drawing.GDIPlus.GdipLoadImageFromStream(System.Runtime.InteropServices.ComTypes.IStream,intptr&)
    48.   at System.Drawing.Image.InitFromStream (System.IO.Stream stream) [0x000c7] in <2853fe23daea44619252c26e80ec312d>:0
    49.   at System.Drawing.Image.LoadFromStream (System.IO.Stream stream, System.Boolean keepAlive) [0x00011] in <2853fe23daea44619252c26e80ec312d>:0
    50.   at System.Drawing.Image.FromStream (System.IO.Stream stream) [0x00000] in <2853fe23daea44619252c26e80ec312d>:0
    51.   at ScreenShotRect+<takeScreenShot>d__15.MoveNext () [0x001af] in E:\Never delete\Unity projekter\Text Generator\Assets\ScreenShotRect.cs:76
    52.   at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00028] in C:\buildslave\unity\build\Runtime\Export\Scripting\Coroutines.cs:17
    53.  
    54. (Filename: <2853fe23daea44619252c26e80ec312d> Line: 0)
     
  2. KingoJH

    KingoJH

    Joined:
    Nov 6, 2015
    Posts:
    27
    I tried to use the windows/forms dlls. Added them to assets/plugins. Set the project to .net 4( or .net framework instead of 2.1), but I receive an error of Requested Clipboard operation did not succeed.Anyone else with this issue or who has managed to get clipboarding to work?