Back to Devexpress

How to: Export a Map

wpf-18267-controls-and-libraries-map-control-examples-printing-and-exporting-how-to-export-a-map.md

latest8.9 KB
Original Source

How to: Export a Map

  • Jun 07, 2019
  • 3 minutes to read

To export a map image to a file, use one of the following methods.

csharp
using System;
using System.Windows;
using DevExpress.XtraPrinting;

namespace Exporting {
    public partial class MainWindow : Window {
        const string filename = "exportedMap";

        public MainWindow() {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e) {
            ExportFormat format = (ExportFormat)cbExportFormat.SelectedItem;
            string filepath;
            if (format == ExportFormat.Image)
                filepath = String.Format("{0}.jpg", filename);
            else
                filepath = String.Format("{0}.{1}", filename, format);
            bool isExported = true;
            switch (format) { 
                case (ExportFormat.Htm):
                    mapControl.ExportToHtml(filepath);
                    break;
                case (ExportFormat.Image):
                    mapControl.ExportToImage(filepath);
                    break;
                case (ExportFormat.Mht): 
                    mapControl.ExportToMht(filepath);
                    break;
                case (ExportFormat.Pdf): 
                    mapControl.ExportToPdf(filepath);
                    break;
                case (ExportFormat.Rtf): 
                    mapControl.ExportToRtf(filepath);
                    break;
                case (ExportFormat.Xls): 
                    mapControl.ExportToXls(filepath);
                    break;
                case (ExportFormat.Xlsx): 
                    mapControl.ExportToXlsx(filepath);
                    break;
                case (ExportFormat.Xps): 
                    mapControl.ExportToXps(filepath);
                    break;
                default: 
                    isExported = false;
                    break;
            }
            if (isExported) 
                MessageBox.Show(String.Format("Map exported successfully."));
            else
                MessageBox.Show(String.Format("Map exporting does not support the {0} file format.", format)); 
        }
    }
}
xaml
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol" 
        xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" 
        xmlns:dxm="http://schemas.devexpress.com/winfx/2008/xaml/map"
        xmlns:dxp="clr-namespace:DevExpress.XtraPrinting;assembly=DevExpress.Printing.v14.2.Core"
        x:Class="Exporting.MainWindow"
        Title="MainWindow" Height="600" Width="800">
    <Window.Resources>
        <ObjectDataProvider x:Key="ExportFormatValues" 
                            MethodName="GetValues"
                            ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="dxp:ExportFormat"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        <ObjectDataProvider x:Key="MapPrintSizeModeValues" 
                            MethodName="GetValues"
                            ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="dxm:MapPrintSizeMode"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>

    <Grid>
        <dxlc:LayoutControl Orientation="Vertical">
            <dxlc:LayoutItem VerticalAlignment="Stretch">
                <dxm:MapControl x:Name="mapControl">
                    <dxm:MapControl.PrintOptions>
                        <dxm:MapPrintOptions SizeMode="{Binding SelectedItem, ElementName=cbPrintSizeMode}"/>
                    </dxm:MapControl.PrintOptions>
                    <dxm:ImageTilesLayer>
                        <dxm:ImageTilesLayer.DataProvider>
                            <dxm:BingMapDataProvider BingKey="Your Bing Key"/>
                        </dxm:ImageTilesLayer.DataProvider>
                    </dxm:ImageTilesLayer>
                </dxm:MapControl>
            </dxlc:LayoutItem>
            <dxlc:LayoutGroup>
                <dxlc:LayoutItem Label="Print Size Mode:">
                    <ComboBox x:Name="cbPrintSizeMode"
                              ItemsSource="{Binding Source={StaticResource MapPrintSizeModeValues}}"
                              SelectedIndex="0"/>
                </dxlc:LayoutItem>
                <dxlc:LayoutItem Label="Export to" AddColonToLabel="True">
                    <ComboBox x:Name="cbExportFormat"  
                        ItemsSource="{Binding Source={StaticResource ExportFormatValues}}"
                        SelectedIndex="0"/>
                </dxlc:LayoutItem>
                <dxlc:LayoutItem Width="100">
                    <Button Content="Export" Click="Button_Click"/>
                </dxlc:LayoutItem>
            </dxlc:LayoutGroup>
        </dxlc:LayoutControl>
    </Grid>
</Window>
vb
Imports System
Imports System.Windows
Imports DevExpress.XtraPrinting

Namespace Exporting
    Partial Public Class MainWindow
        Inherits Window

        Private Const filename As String = "exportedMap"

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Dim format As ExportFormat = CType(cbExportFormat.SelectedItem, ExportFormat)
            Dim filepath As String
            If format = ExportFormat.Image Then
                filepath = String.Format("{0}.jpg", filename)
            Else
                filepath = String.Format("{0}.{1}", filename, format)
            End If
            Dim isExported As Boolean = True
            Select Case format
                Case (ExportFormat.Htm)
                    mapControl.ExportToHtml(filepath)
                Case (ExportFormat.Image)
                    mapControl.ExportToImage(filepath)
                Case (ExportFormat.Mht)
                    mapControl.ExportToMht(filepath)
                Case (ExportFormat.Pdf)
                    mapControl.ExportToPdf(filepath)
                Case (ExportFormat.Rtf)
                    mapControl.ExportToRtf(filepath)
                Case (ExportFormat.Xls)
                    mapControl.ExportToXls(filepath)
                Case (ExportFormat.Xlsx)
                    mapControl.ExportToXlsx(filepath)
                Case (ExportFormat.Xps)
                    mapControl.ExportToXps(filepath)
                Case Else
                    isExported = False
            End Select
            If isExported Then
                MessageBox.Show(String.Format("Map exported successfully."))
            Else
                MessageBox.Show(String.Format("Map exporting does not support the {0} file format.", format))
            End If
        End Sub
    End Class
End Namespace
vb
Imports System
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Threading.Tasks
Imports System.Windows

Namespace Exporting
    ''' <summary>
    ''' Interaction logic for App.xaml
    ''' </summary>
    Partial Public Class App
        Inherits Application

    End Class
End Namespace
xaml
<Application x:Class="Exporting.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>