New to Telerik UI for WPF? Download free 30-day trial

Export Support

More often than not, it is a good idea to be able to preserve the data that the different controls show and edit during the life cycle of the application even after the application is closed. There are different ways to save this information and various approaches can be adopted depending on the type of the content.

Built-In Export

Several of the controls in the Telerik UI for WPF suite come with built-in export capabilities. Among those are RadDiagram, RadGridView and RadRichTextBox.

To learn more about these abilities take a look at the Export article in the desired control's documentation.

Document Processing Integration

The Telerik UI for WPF suite includes the Telerik Document Processing libraries specifically designed for import, export and document editing.

The libraries give you the ability to create a document from scratch and export it to its supported file formats. This means you can export practically any control either by exporting it to an image and adding the image to the resulting document or by creating an appropriate for the context structure (for example, a table when exporting RadGridView to DOCX).

There are several controls that already provide sample code which you can use as the base of export functionality, you could take a look at them in the Telerik XAML SDK repository:

Export XAML UI Elements to PDF

The API of RadPdfProcessing is designed to resemble XAML and this allows you easy conversion of UI elements to PDF by converting any XAML primitive to a PDF instruction. For base of such conversion you can use the RadPdfProcessing Export UI Element to PDF example which demonstrates how to export several of the controls in the Telerik UI for WPF suite, including a combination of several controls in the same view.

Common Export Support

The code operates with a set of renderers deriving from the base UIElementRendererBase - TextBlockRenderer, BorderRenderer, etc. This allows separation, since each concrete render is responsible for drawing the element it is intended for without dependencies to the other renderers, and gives you the ability to extend the sample code to fit your precise needs if you need to.

Take a look at the source code of the example on GitHub and the documentation of the relevant FixedDocumentEditor class.

Export Images With ExportExtensions

Some controls can be exported directly using the ExportExtensions class which is part of the Telerik.Windows.Controls assembly. It allows you to export in several image formats listed below:

Image formats

  • Png: Portable Netwok Graphic. Use ExportToImage(FrameworkElement, Stream) method.

  • Bmp: Bitmap file. Use ExportToImage(FrameworkElement, Stream, BitmapEncoder) where the encoder is of type BmpBitmapEncoder.

  • Xps: XML Paper Specification file. Use ExportToXpsImage(FrameworkElement, Stream) method to export content as an XPS image.

This approach is convenient for controls which have a size that allows direct export on one page, such as a RadGauge for example.

There are overloads for the methods listed above, which take as parameter the name of the file you want to export to, instead of a stream. This allows you to easily export your control directly to the file system.

Example 1 demonstrates how to export RadGauge to PNG file format. The physical path to the image is provided run-time via SaveFileDialog:

Example 1: Export Control to PNG

 private void Button_Click(object sender, RoutedEventArgs e) 
    string extension = "png"; 
    SaveFileDialog dialog = new SaveFileDialog() 
        DefaultExt = extension, 
        Filter = "Png (.png)|.png" 
    if (dialog.ShowDialog() == true) 
        using (Stream stream = dialog.OpenFile()) 
                 this.radGauge, stream, new System.Windows.Media.Imaging.PngBitmapEncoder()); 
 Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) 
    Dim extension As String = "png" 
    Dim dialog As New SaveFileDialog() With {.DefaultExt = extension, .Filter = "Png (.png)|.png"} 
    If dialog.ShowDialog() = True Then 
        Using stream As Stream = dialog.OpenFile() 
            Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(Me.radGauge, stream, New System.Windows.Media.Imaging.PngBitmapEncoder()) 
        End Using 
    End If 
 End Sub 

Exporting a control to an image requires that the control is measured and arranged. Otherwise, unexpected results may occur.

See Also

In this article