title: DexExpress XPF Map in Windows Service
body: <p>I'm using the DexExpress.Xpf.Map.v15.1 MapControl and trying to create code that will be able to run in a Windows Service (i.e. no front end) to generate maps as images.</p>
  
  <p>I have the following code which creates a map, adds GeoPoints to a PolyLine and then adds that PolyLine to a map. This works fine in my WPF test harness application:</p>
  
  <pre><code> public MapWindow1()
          {
              InitializeComponent();
  
              var mapControl = new MapControl
              {
                  Name = "TheMap",
                  ZoomLevel = 4,
                  CenterPoint = new GeoPoint(47, 5)
              };
  
              var imageLayer = new ImageTilesLayer
              {
                  DataProvider = new OpenStreetMapDataProvider()
              };
              mapControl.Layers.Add(imageLayer);
  
              var vectorLayer = new VectorLayer();
              mapControl.Layers.Add(vectorLayer);
              var mapItemStorage = new MapItemStorage();
              vectorLayer.Data = mapItemStorage;
  
              var polyLineCollection = DbfGenerator.GeneratePolyLineCollection();
  
              polyLineCollection.ForEach(line =&gt;
              {
                  var mapItem = new MapPolyline();
                  mapItemStorage.Items.Add(mapItem);
                  mapItem.Points.AddRange(line);
              });
              mapControl.MouseDoubleClick += mapControl_MouseDoubleClick;
  
              MainGrid.Children.Add(mapControl);
  
          }
  </code></pre>
  
  <p>This runs and adds the map control to the WPF page without a problem. I've then added double click handler to export the map as an image:</p>
  
  <pre><code>void mapControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
          {
              var m = GenerateMapPng((MapControl) sender);
              m.Save(@"C:\0Temp\test.bmp");
          }
  
   public Bitmap GenerateMapPng(MapControl map)
      {
          Bitmap bmp;
          // export image from map
          using (MemoryStream ms = new MemoryStream())
          {
              var exportOptions = new ImageExportOptions(ImageFormat.Png);
              map.ExportToImage(ms, exportOptions);
  
  
              ms.Position = 0;
              bmp = new Bitmap(System.Drawing.Image.FromStream(ms));
          }
          return bmp;
      }
  </code></pre>
  
  <p>Again - this works without a problem. 
  However, If I remove the UI element and run the following code:</p>
  
  <pre><code>public MapWindow1()
  {
      InitializeComponent();
  
  
      var mapControl = new MapControl
      {
          Name = "TheMap",
          ZoomLevel = 4,
          CenterPoint = new GeoPoint(47, 5)
      };
  
  
      var imageLayer = new ImageTilesLayer
      {
          DataProvider = new OpenStreetMapDataProvider()
      };
      mapControl.Layers.Add(imageLayer);
  
      var vectorLayer = new VectorLayer();
      mapControl.Layers.Add(vectorLayer);
      var mapItemStorage = new MapItemStorage();
      vectorLayer.Data = mapItemStorage;
  
      var polyLineCollection = DbfGenerator.GeneratePolyLineCollection();
  
      polyLineCollection.ForEach(line =&gt;
      {
          var mapItem = new MapPolyline();
          mapItemStorage.Items.Add(mapItem);
          mapItem.Points.AddRange(line);
      });
  
      var m = GenerateMapPng(mapControl);
      m.Save(@"C:\0Temp\test.bmp");
  
  }
  </code></pre>
  
  <p>I get a NullReferenceException on the "<code>map.ExportToImage(ms, exportOptions);</code>" line.</p>
  
  <p>I'm assuming that when the WPF application loads it calls a method to initialise the map, but I can't find anything that I can manually do to the control (Load/Init method) to mimic this. </p>
  
  <p>Is there a way to trick the MapControl in to thinking it is in a WPF page when it isn't?</p>