Create Graph Image and save it to local file system

Dec 5, 2012 at 6:07 AM


I am using NodeXL libraries for WPF and it work fine for creating graphs. 

I am trying to save the graph as an image on the file system using following code

System.Windows.Forms.SaveFileDialog s = new System.Windows.Forms.SaveFileDialog();
                s.FileName = "Graph For " + lblTargetName.Content.ToString();
                s.Filter = "Gif Files | *.gif |Png Files |*.png|Jpeg Files |*.jpg |Bmp Files |*.bmp|Tiff Files |*.tiff";
                s.DefaultExt = "gif";
                var result = s.ShowDialog();
                if (s.FileName != "" && result == System.Windows.Forms.DialogResult.OK)
                    var extension = System.IO.Path.GetExtension(s.FileName);
                    NodeXLControl.GraphImageCenterer ImgCenter = new NodeXLControl.GraphImageCenterer(nodeXLControl1);
                    System.Windows.Size size= new Size();
                    size.Height = 0;
                    size.Width = 0;                   
                    System.Drawing.Bitmap img = nodeXLControl1.CopyGraphToBitmap(4500,4500);
                    switch (extension.ToLower())
                        case ".gif":
                            img.Save(s.FileName, System.Drawing.Imaging.ImageFormat.Gif); 
                        case ".png":
                            img.Save(s.FileName, System.Drawing.Imaging.ImageFormat.Png); 
                        case ".jpg":
                            img.Save(s.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                        case ".bmp":
                            img.Save(s.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
                        case ".tiff":
                            img.Save(s.FileName, System.Drawing.Imaging.ImageFormat.Tiff);
                            throw new ArgumentOutOfRangeException(extension);


It saves the graph in specified image format but leaves a horizontal black line on the top of image and cut the portion of graph from bottom in image.

Am i doing something wrong in drawing the image ?

Dec 6, 2012 at 6:27 AM
Edited Dec 6, 2012 at 6:46 AM

It's a bug in the NodeXLControl that I never noticed before.  It occurs only in WPF applications.  It does not occur in Windows Forms applications in which the NodeXLControl is hosted within an ElementHost control.  Our own application that uses the control is Windows Forms-based.

I've recorded the bug, but I'm not sure when I'll have the time to track it down and fix it.  I have two suggestions:

1. I can get the CopyGraphToBitmap() method to work fine in a WPF application by hosting the NodeXLControl inside an ElementHost, which in turn is hosted inside a WindowsFormsHost.  So the control hierarchy is Window -> WindowsFormsHost -> ElementHost -> NodeXLControl.  Nutty and convoluted, but it worked.  I don't know if this would have side effects in your application, though.

2. If you're feeling ambitious, you could download the source code and track down the bug yourself by stepping through the CopyGraphToBitmap() method.  If you come up with a fix, or even just an explanation of why the method creates black bands when the control is not hosted in an ElementHost, we would love to incorporate it and give you full credit.

In any case, thanks for reporting the bug.

-- Tony