Is it possible to layout and export the graphs as images without hosting the NodeXLControl in a WPF app?
I'm able to do this with a WPF application, but I'd like to automate it without bringing up a window. By the way, thanks for all the comments in code, they are very helpful :)
Also, any way to layout the graph and export it as an SVG in code?
Yes, you can lay out a graph and export it as an image without using the NodeXLControl. You should use the NodeXLVisual class. which is a lower-level, windowless component in the NodeXL Class Libraries. In the "NodeXLVisual Class" topic in the
NodeXLApi.chm help file, there is a complete code sample that shows you how to do it. The sample is for an ASP.NET page, but you can easily adapt it to your own needs. I'll include the sample in my next post.
We don't support exporting to SVG, however. The closest you can get is to export to XPS (which we
do support), and then converting the XPS to SVG. There are third-party solutions for doing that, although I've never used any of them.
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
const Int32 GraphWidth = 200;
const Int32 GraphHeight = 100;
// The graph will be written to the response stream as a Gif.
Response.ContentType = "image/gif";
// Create a graph. The graph has no visual representation; it is just
// a data structure.
Graph oGraph = new Graph(GraphDirectedness.Directed);
IVertexCollection oVertices = oGraph.Vertices;
IEdgeCollection oEdges = oGraph.Edges;
// Add three vertices.
IVertex oVertexA = oVertices.Add();
oVertexA.Name = "Vertex A";
IVertex oVertexB = oVertices.Add();
oVertexB.Name = "Vertex B";
IVertex oVertexC = oVertices.Add();
oVertexC.Name = "Vertex C";
// Connect the vertices with directed edges.
IEdge oEdge1 = oEdges.Add(oVertexA, oVertexB, true);
IEdge oEdge2 = oEdges.Add(oVertexB, oVertexC, true);
IEdge oEdge3 = oEdges.Add(oVertexC, oVertexA, true);
// Synchronously lay out the graph using one of the NodeXL-supplied
// layout objects.
ILayout oLayout = new FruchtermanReingoldLayout();
LayoutContext oLayoutContext = new LayoutContext(
new Rectangle(0, 0, GraphWidth, GraphHeight) );
// Create an object that can render a NodeXL graph as a Visual.
NodeXLVisual oNodeXLVisual = new NodeXLVisual();
// Use the NodeXLVisual object's GraphDrawer to draw the graph onto the
GraphDrawingContext oGraphDrawingContext = new GraphDrawingContext(
new Rect(0, 0, GraphWidth, GraphHeight), oLayout.Margin,
System.Windows.Media.Color.FromRgb(255, 255, 255) );
// Convert the Visual to a Bitmap.
RenderTargetBitmap oRenderTargetBitmap = new RenderTargetBitmap(
GraphWidth, GraphHeight, 96, 96, PixelFormats.Default);
BmpBitmapEncoder oBmpBitmapEncoder = new BmpBitmapEncoder();
oBmpBitmapEncoder.Frames.Add( BitmapFrame.Create(oRenderTargetBitmap) );
MemoryStream oMemoryStream = new MemoryStream();
Bitmap oBitmap = new Bitmap(oMemoryStream);
// Write the Bitmap's contents to the response stream.