Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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'm using MMA 8.0 to provide backend on a .NET application I'm writing. I am trying to use the high-level Wolfram.NETLink.MathKernel interface to export an image created with GraphPlot[].

Although the Result comes back as '-Graphics-', I find that Graphics[] is empty. Changing the Compute() to another graphic generating command returns an image in the Graphic[] as expected.

Here's my example code:

Wolfram.NETLink.IKernelLink _iKernelLink;

string cmdLine = "-linkmode launch -linkname \"C:/Program Files/Wolfram Research/Mathematica/8.0/MathKernel.exe\"";
_iKernelLink = Wolfram.NETLink.MathLinkFactory.CreateKernelLink(cmdLine);

var k = new Wolfram.NETLink.MathKernel(_iKernelLink); // new MathKernel

k.AutoCloseLink = true; // close link when object is disposed
k.CaptureGraphics = true; // capture graphics output
k.CapturePrint = true; // capture print output
k.CaptureMessages = true; // capture messages
k.HandleEvents = true; // lets Compute thread handle application events
k.UseFrontEnd = false; // don't use Mathematica Notebook front-end

// this produces empty Graphic[] result
string command = @"GraphPlot[{1 -> 2, 2 -> 1, 3 -> 1, 3 -> 2, 4 -> 1, 4 -> 2, 4 -> 4}]";

// this command produces the expected Graphic[] result
//string command = @"BarChart@Range[5]";


if (k.Graphics.Length > 0) // <-- should be an image for both commands
    return k.Graphics[0];
    return null;
share|improve this question
I have just seen that you use UseFrontEnd = false; I could very well imagine that some parts of the Graph rendering need a frontend, so I'd try to set that to yes when experimenting... – Albert Retey Jan 25 '12 at 20:42
Hello James and welcome to the Mathematica StackExchange. We are a pretty friendly bunch! Don't forget to upvote good answers (and other people's questions) using the triangle above the number next to the post. – Verbeia Jan 25 '12 at 22:52
up vote 4 down vote accepted

I think your problem is that Graph will actually not evaluate to a Graphics but only be rendered as something like that by the FrontEnd. You could look at ToBoxes[yourgraph] to learn some details and try to extract something that can be transfered to .NET. I don't have any experience with NETLink, but I remember that at least older versions of JLink would return graphics as rastered images anyway, so a more simple approach could be to use

Rasterize[GraphPlot[{1 -> 2, 2 -> 1, 3 -> 1, 3 -> 2, 4 -> 1, 4 -> 2, 4 -> 4}]]

You might want to finetune this with the options of Rasterize to meet your needs.

share|improve this answer
Show will convert Graph to Graphics: try Show@Graph[{1 -> 2}]. – Szabolcs Jan 25 '12 at 20:43
@Szabolcs: good point, I tried to find the function to do that but didn't find the most obious (tried Normal and some others). Why don't you make that an answer, isn't it just exactly what the OP needs? I'll be happy to add it to mine otherwise :-) – Albert Retey Jan 25 '12 at 21:39
I tried both Show and Rasterize. Interestingly, both cause a front-end to open (in spite of UseFrontEnd setting), but only Show seems to actually work (i.e., Graphics[] result is filled with the GraphPlot). – James Brunner Jan 25 '12 at 22:41

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.