5
$\begingroup$

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()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            /*
                MathKernel kernel = new MathKernel();
                kernel.Compute("ExportString[Graphics[Rectangle[]],{\"Base64\",\"EMF\"}]");
                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.Compute("Show[Graphics[Rectangle[]]]");
            k.Graphics[0].Save("C:\\temp\\file.gif", System.Drawing.Imaging.ImageFormat.Gif);
        }
    }
}
$\endgroup$
1
  • $\begingroup$ I've tried the same thing in C# and Oxygene .Net. Both have the same problem. $\endgroup$
    – sav
    Jul 3, 2013 at 2:19

2 Answers 2

6
$\begingroup$


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.

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

To transfer an EMF graphic you can use this:

kernel.Compute("ExportString[Graphics[Rectangle[]],{\"Base64\",\"EMF\"}]");
byte[] decodedBytes = Convert.FromBase64String(kernel.Result.ToString());
File.WriteAllBytes("C:\\Temp\\output.emf", decodedBytes);

Ref: https://stackoverflow.com/questions/7755810/converting-graphics-with-exportstring

also: https://stackoverflow.com/questions/7542828/netlink-graphics-producing-png-instead-of-emf

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

MathKernel k = new MathKernel();
k.CaptureGraphics = true;
k.GraphicsFormat = "GIF";
k.Compute("Show[Graphics[Rectangle[]]]");
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.

Ref http://reference.wolfram.com/legacy/v7/NETLink/ref/net/Wolfram.NETLink.MathKernel.Graphics.html

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – sav
    Jul 2, 2013 at 23:43
  • $\begingroup$ 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." $\endgroup$
    – sav
    Jul 2, 2013 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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