Print all Vertexes?

Dec 15, 2010 at 8:33 AM

Hey,

I'd like to be able to save each vertex of my graph to  a seperate image file.

I have code to print a FrameworkElement
However i'm struggling to work out how to access the FrameworkElement's which correspond to the vertex?

this seems really simple - any pointers?

David

 

Dec 15, 2010 at 10:10 AM

ok worked it out..

 

public void PrintMe() {
            UIElementCollection uiObjects = GraphLayout.Children;

  	  SaveFileDialog saveFialog = new SaveFileDialog { DefaultExt = ".png" };
            if (saveFialog.ShowDialog() == true) {
                int i = 1;
                string fname = saveFialog.FileName.Replace(".png""");

                foreach (object child in uiObjects) {// edges or vertexes

                    VertexControl vertexControl = child as VertexControl;
                    if (vertexControl != null) { // we just want vertexes

                        int children = VisualTreeHelper.GetChildrenCount((DependencyObject)child);
                        for (int c = 0; c < children; c++) { // vertexes dont seem to print so we find their visual child - normally a border or grid or something
                            var child1 = VisualTreeHelper.GetChild((DependencyObject)child, c);

                            var target = (Visual)child1;
                            var frameworkElem = (FrameworkElement)child1;

                            //render it visually to a bitmap
                            RenderTargetBitmap rtb = new RenderTargetBitmap((int)frameworkElem.ActualWidth, 
                                                                            (int)frameworkElem.ActualHeight, 96, 96,
                                                                            PixelFormats.Default);
                            rtb.Render(target);

                            // & save it as a png :)
                            PngBitmapEncoder png = new PngBitmapEncoder();

                            png.Frames.Add(BitmapFrame.Create(rtb));

                            using (Stream stm = File.Create(fname + i + ".png")) {
                                png.Save(stm);
                            }

                            i++;

                        }

                    }
                }
            }
        }
hope that helps people - do post here if it does :)