5

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);
        }
    }
}
Peter Mortensen
  • 759
  • 4
  • 7
sav
  • 153
  • 4

2 Answers2

7

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

Chris Degnen
  • 30,927
  • 2
  • 54
  • 108
  • 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 02 '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 02 '13 at 23:47
6


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.

Verbeia
  • 34,233
  • 9
  • 109
  • 224
Jonie
  • 1,199
  • 6
  • 14
  • CaptureGraphics Is set to true. I'm using Import["image.png"] for my Mathematica command. mathKernel.Graphics.Length is still zero – sav Jul 02 '13 at 06:04
  • Perhaps I need the full version of Mathematica to do this – sav Jul 02 '13 at 06:06
  • Try this: Show[Import["Image.png"]]

    Make sure that your .Result doesn't say $Failed as well.

    – Jonie Jul 02 '13 at 06:36
  • Tried this, mathKernel.Result is null, and mathKernel.Graphics.Length is zero – sav Jul 02 '13 at 06:42
  • Now the Result says -Image- in the debugger. but when I cast it as System.Drawing.Image the result is null – sav Jul 02 '13 at 06:48
  • if it says Image in debugger, then you should check the Graphics whether it still says null. – Jonie Jul 02 '13 at 06:50
  • may I request that you post your .NET code so we could get a better understanding of what may be happening? – Jonie Jul 03 '13 at 00:19
  • Okay, I've confirmed that the GIF code in your edit works for me. k.Result = "-Graphics-", and k.Graphics = {System.Drawing.Image[1]}. Looks like you do need the full version of Mathematica. – Jonie Jul 03 '13 at 03:44
  • As a workaround, you could try using Export[...] in Mathematica to get Mathematica to export the file first into your file system. Then load the file back in C#. – Jonie Jul 03 '13 at 05:06
  • Yep, tried that, it seems export is disabled in the trial version. – sav Jul 03 '13 at 05:16