# How to access graphics from .Net in mathematica?

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.Windows.Forms;
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);

}
}
}

-
I've tried the same thing in C# and Oxygene .Net. Both have the same problem. –  sav Jul 3 '13 at 2:19

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. - 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
show 5 more comments

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


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.

-
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