Copy image to clipboard

Hey guys

I am working on a system that has a orthographic camera in the scene. Is there anyone that can give me pointers how I can save the camera view to a jpeg image (that I am fine with) and then copy the data to a clipboard to be pastes outside Unity?

Also, the project only works with Unity standalone inside Windows.

Any guidelines appreciated, thanks!

Maybe this. I haven’t tried it though;

Oh, that saves it to a file.

Thanks F7S

Ok, so I figured it out:)
here goes:

copy the (mono version) System.Windows.Forms as well as System.Drawing dll’s into your Plugins folder
Then reference them in the top of your script
Then place the following as a test script in the start function of a unity script

void Start ()
    {
        using (MemoryStream ms = new MemoryStream())
        {

            Bitmap bm = new Bitmap(200, 100);
            System.Drawing.Graphics flagGraphics = System.Drawing.Graphics.FromImage(bm);
            int red = 0;
            int white = 11;
            while (white <= 100)
            {
                flagGraphics.FillRectangle(Brushes.Red, 0, red, 200, 10);
                flagGraphics.FillRectangle(Brushes.White, 0, white, 200, 10);
                red += 20;
                white += 20;
            }

            Clipboard.SetImage(bm);
        }
    }

Attach and run the script and if you paste in windows paint, you should see an image with red lines. Just tested it and works perfect. Granted I have not tested it in a standalone build, but should work.

1 Like

Any info about iOS & Android?

It took me hours to figure it out, but basically if you want to copy an image from a standalone windows app (C#), just use the following:

string imagePath = @"C:\Path\To\Your\image.png";
string script = $@"
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$img = [System.Drawing.Image]::FromFile('{imagePath}')
[System.Windows.Forms.Clipboard]::SetImage($img)
";
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo {
    FileName = "powershell",
    Arguments = $"-Command \"{script}\"",
    UseShellExecute = false,
    CreateNoWindow = true
});

Basically, you have to save the image somewhere in your folders (can be %temp% or any folder). Then you access it with the code above and use Powershell to copy it to the Clipboard. Then you can delete it from the temp folder that you put it in.

Do not try to use these… It took me hours messing with them, and no chance because Unity threat does not work properly with that.
System.Drawing
System.Windows.Media.Imaging