# Using NETLink.MathKernel.Compute() for graphics results fails with GraphPlot[]

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;

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]";

k.Compute(command);

if (k.Graphics.Length > 0) // <-- should be an image for both commands
return k.Graphics[0];
else
return null;

-
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

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

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