Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

How do I convert a .NET STAThread into .NET/Link Mathematica code? Alternatively what would be the easiest way to compile and run the program below from within mathemaitca?

using System;
using Wolfram.NETLink;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

public class File{
 public static void Main(String[] args) {
    MathKernel k = new MathKernel();
    k.Compute("ExportString[Graphics[Circle[]],{\"Base64\",\"PNG\"}, Background -> None]");
    string input = k.Result.ToString();

    byte[] bytes = Convert.FromBase64String(input); 
    Stream stream = new MemoryStream(bytes);
    IDataObject dataObject = new DataObject();
    dataObject.SetData("PNG", stream);
    Clipboard.SetDataObject(dataObject, true);

I am compiling it using the following code, in case anyone wants to try it out.

csc /target:winexe /reference:Wolfram.NETLink.dll File.cs
share|improve this question

1 Answer 1

up vote 0 down vote accepted

All code in Mathematica seems to be running in STAThread by default.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.