Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to use Wolfram.NETLink.MathKernel.Graphics to retrieve an image generated from a Mathematica command. Which Mathematica command do I use to do this?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Wolfram.NETLink;
using System.IO;

namespace WindowsFormsApplication1
    public partial class Form1 : Form
        public Form1()

        private void button1_Click(object sender, EventArgs e)
                MathKernel kernel = new MathKernel();
                byte[] decodedBytes = Convert.FromBase64String(kernel.Result.ToString());
                File.WriteAllBytes("C:\\temp\\output.emf", decodedBytes);

            MathKernel k = new MathKernel();
            k.CaptureGraphics = true;
            k.GraphicsFormat = "GIF";
            k.Graphics[0].Save("C:\\temp\\file.gif", System.Drawing.Imaging.ImageFormat.Gif);
share|improve this question
I've tried the same thing in C# and Oxygene .Net. Both have the same problem. – sav Jul 3 '13 at 2:19
up vote 6 down vote accepted

I'm presuming that you already have a command which generates a graphics and you're trying to retrieve it through the MathKernel. If so, try the following:
1. Set the CaptureGraphics Property on your MathKernel to true.
2. Run MathKernel.Compute(command)
3. If your command successfully returned a graphics (you could check by Mathematica notebook), you should be able to retrieve it inside the MathKernel.Result.

Edit: My apologies, MathKernel.Result would tell you whether it succeeded or not (with $Failed signifying a failure). If it succeeded, you can retrieve it via .Graphics.
I'd also like to point out for anyone who may be dealing with this, that I've had to use the function UseFrontEnd quite frequently so that the graphics renders correctly.

share|improve this answer
CaptureGraphics Is set to true. I'm using Import["image.png"] for my Mathematica command. mathKernel.Graphics.Length is still zero – sav Jul 2 '13 at 6:04
Perhaps I need the full version of Mathematica to do this – sav Jul 2 '13 at 6:06
Try this: Show[Import["Image.png"]] Make sure that your .Result doesn't say $Failed as well. – Jonie Jul 2 '13 at 6:36
Tried this, mathKernel.Result is null, and mathKernel.Graphics.Length is zero – sav Jul 2 '13 at 6:42
Now the Result says -Image- in the debugger. but when I cast it as System.Drawing.Image the result is null – sav Jul 2 '13 at 6:48

To transfer an EMF graphic you can use this:

byte[] decodedBytes = Convert.FromBase64String(kernel.Result.ToString());
File.WriteAllBytes("C:\\Temp\\output.emf", decodedBytes);



GIF or JPEG graphics can be transferred via the MathKernel.Graphics property.

MathKernel k = new MathKernel();
k.CaptureGraphics = true;
k.GraphicsFormat = "GIF";
k.Graphics[0].Save("C:\\Temp\\file.gif", System.Drawing.Imaging.ImageFormat.Gif);

Note Graphics is an array. Multiple images can be transferred in one operation.


share|improve this answer
Thanks for the response, but unfortunately this still does not work. I'm getting an index out of range exception because k.Graphics does not have any images. – sav Jul 2 '13 at 23:43
The code snippet for Base64 encoding also gives an exception: "An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters." – sav Jul 2 '13 at 23:47

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.