This is a migrated thread and some comments may be shown as answers.

Export multiple Diagrams to PNG

1 Answer 54 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Ciro
Top achievements
Rank 1
Ciro asked on 04 Apr 2014, 04:10 PM
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


     

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 08 Apr 2014, 01:54 PM
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.

 
Tags
Diagram
Asked by
Ciro
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or