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.

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.

EDIT:

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);

    }
}
}
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

2 Answers 2

up vote 4 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:

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

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

also: http://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

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

 
discard

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.