Export multiple Diagrams to PNG

2 posts, 0 answers
  1. Ciro
    Ciro avatar
    11 posts
    Member since:
    Nov 2013

    Posted 04 Apr 2014 Link to this post

    Hi,

    I have this scenario: I want to create from code behind some RadDiagrams, then export every diagram to a single PNG image without open and display the raddiagram.

    Below there is my two solutions of code which don't  work. IN particular, the stream is always empty.
    1)

            var myGraphSource = new GraphSource(); //this contains a list of shapes and connections
           var diagram = new RadDiagram();
           diagram.GraphSource = myGraphSource ;
            diagram.SelectAll();
             using (var stream = new MemoryStream()) {
                           diagram.ExportToImage(stream, margin: new Thickness(0,0,0,0), backgroundBrush: new SolidColorBrush(Colors.White));
                            image = stream.ToArray();
            }      
    2) 


                        var diagramViewModel = new DiagramViewModel(myGraphSource);
                        var diagramView = new DiagramView();
                        diagramView.DataContext = diagramViewModel;
        using (var stream = new MemoryStream()) {
                           diagramView.diagram.ExportToImage(stream, margin: new Thickness(0,0,0,0), backgroundBrush: new SolidColorBrush(Colors.White));
                            image = stream.ToArray();
                        }

    In both cases the code doesn't work and in the last scenario, I' ve bounded the GraphSource in the xaml. Is there a way to get my issue????
    Thanks

    Ciro


         
  2. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 08 Apr 2014 Link to this post

    Hi Ciro,

    The reported issue is caused by the fact that your diagram is not added in the visual tree. The service that exports the diagram to an image takes a snapshot of the visual tree and then save it. In order to export your diagram correctly you can add it in your layout and then export it in its Loaded event handler. Here is an example in code:
    var myGraphSource = new GraphSource(); //this contains a list of shapes and connections
    var diagram = new RadDiagram();
    diagram.GraphSource = myGraphSource;
    diagram.SelectAll();
    diagram.Loaded += diagram_Loaded;
    layoutGrid.Children.Add(diagram);
     
    ...................................................
     
    void diagram_Loaded(object sender, RoutedEventArgs e)
    {
        using (var stream = new MemoryStream())
        {
            ((RadDiagram)sender).ExportToImage(stream, margin: new Thickness(0, 0, 0, 0), backgroundBrush: new SolidColorBrush(Colors.White));
            var image = stream.ToArray();
        }   
    }

    Please let me know if you need any further assistance.


    Regards,
    Martin
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top