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;
  • $\begingroup$ 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... $\endgroup$ – Albert Retey Jan 25 '12 at 20:42
  • $\begingroup$ 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. $\endgroup$ – Verbeia Jan 25 '12 at 22:52

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.

  • 6
    $\begingroup$ Show will convert Graph to Graphics: try Show@Graph[{1 -> 2}]. $\endgroup$ – Szabolcs Jan 25 '12 at 20:43
  • $\begingroup$ @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 :-) $\endgroup$ – Albert Retey Jan 25 '12 at 21:39
  • $\begingroup$ 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). $\endgroup$ – James Brunner Jan 25 '12 at 22:41
  • $\begingroup$ @Szabolcs This has helped me. I think this should be an answer rather than a comment. $\endgroup$ – Max Dec 14 '19 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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