Printing

Jul 29, 2009 at 7:06 PM

Has anyone tackled printing the graph that is displayed?  Is this possible now or would it require an enhancement?

Oct 9, 2009 at 11:11 AM

Hi TBlackbird,

 

I wrote a primitive class some time ago from diffrent resources on the net.

If anyone want's to use it, here it is:

//With these usings...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuickGraph;
using System.Windows.Media;
using System.Windows;
using System.Windows.Media.Imaging;
using System.IO;


/// <summary>
    /// Class handles operations for printing the graph to image.
    /// </summary>
    class GraphToImage
    {
        //Set resolution of image.
        const double dpi = 96d;

        //Set pixelformat of image.
        PixelFormat pixelFormat = PixelFormats.Pbgra32;

        /// <summary>
        /// Method exports the graphlayout to an png image.
        /// </summary>
        /// <param name="path">destination of image</param>
        /// <param name="surface">graphlayout you want to print</param>
        public void ExportToPng(GraphSharp.Controls.GraphLayout surface, Uri path)
        {
            //Save current canvas transform
            Transform transform = surface.LayoutTransform;

            //Reset current transform (in case it is scaled or rotated)
            surface.LayoutTransform = null;

            //Get the size of canvas
            Size size = new Size(surface.ActualWidth, surface.ActualHeight);
            
            //Measure and arrange the surface
            //VERY IMPORTANT
            surface.Measure(size);
            surface.Arrange(new Rect(size));

            //Create a render bitmap and push the surface to it
            RenderTargetBitmap renderBitmap =
              new RenderTargetBitmap(
                (int)size.Width,
                (int)size.Height,
                dpi,
                dpi,
                pixelFormat);

            //Render the graphlayout onto the bitmap.
            renderBitmap.Render(surface);

            //Create a file stream for saving image
            using (FileStream outStream = new FileStream(path.LocalPath, FileMode.Create))
            {
                //Use png encoder for our data
                PngBitmapEncoder encoder = new PngBitmapEncoder();

                //Push the rendered bitmap to it
                encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                
                //Save the data to the stream
                encoder.Save(outStream);
            }

            //Restore previously saved layout
            surface.LayoutTransform = transform;
        }
    }

And here is where i call the class.

GraphToImage graphToImage = new GraphToImage();
                graphToImage.ExportToPng(graphLayout, new Uri(((SaveFileDialog)sender).FileName));

Hope this points someone in the right direction... but its really basic...

Good luck