Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Copy image to Windows Clipboard

Discussion in 'Code Editors & IDEs' started by Vifi, Feb 3, 2021.

  1. Vifi

    Vifi

    Joined:
    Aug 5, 2013
    Posts:
    28
    Hi,
    for past few days i am working on tool that will make a screenshot and copy it to Windows Clipboard.

    I did read a lot of topics about it.
    From what i understand i need to use
    Code (CSharp):
    1. System.Drawing.Image tempImage = System.Drawing.Image.FromStream(stream);
    2. System.Windows.Forms.Clipboard.SetImage(tempImage);
    I should provide it with stream and it should work just fine.

    My problem is about references.
    using System.Drawing
    and
     using System.Windows.Forms
    doesn't work. Those ares not seen by unity.
    I have compatiblility set for .NET standard 2.0
    I found out solution. It is to copy those library into Assets/Plugins folder. I tried to took those from unity directory blablba/mono/2.0/ as well as from .NET directory blablba/ProgramFiles(x86)/Reference Assemblies/etc.
    Both didn't work.

    Files from system .NET directory does "Loading assembly failed" error but it seems like System.Windows.Forms.Clipboard is working. Unity shows errors about Image type
    error CS0234: The type or namespace name 'Image' does not exist in the namespace 'System.Drawing
    and
    error CS7069: Reference to type 'Image' claims it is defined in 'System.Drawing', but it could not be found


    Files from unity copied into Assets/Plugins gives me an error about duplication of library and command me to delete one.
    error CS1703: Multiple assemblies with equivalent identity have been imported

    I tried to copy dll-s from higher versions like 4.0 or 4.5-api. Those doesn't rise Multiple asemblies error but still doesn't work just like those from Windows .NET directory.

    My next steps was to use
    [DllImport("System.Drawing.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    but i am kinda new to this and i am not sure how to get Image type from it and use FromStream method that returns Image object.

    Does anyone know how to deal with it? Or at least can point me in the right direction?

    Thanks in advance and cheers!
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,254
    i think it works if you copy dll's to project root
     
  3. Vifi

    Vifi

    Joined:
    Aug 5, 2013
    Posts:
    28
    When i copied dll's into project root (there where Asset folder is) it didn't work either.
    When i copied dll's into Assets folder Visual Studio saw it and didn't report error but unity still claiming that Image is not in System.Drawing and cannot be found.
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,254
    works here, did you restart unity?
    upload_2021-2-3_14-7-26.png

    my settings,
    upload_2021-2-3_14-6-15.png

    i dont remember from which folder i copied dll, but its
    upload_2021-2-3_14-7-3.png
     
  5. Vifi

    Vifi

    Joined:
    Aug 5, 2013
    Posts:
    28