Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Take a screenshot

This code sample is available for these platforms:
View Sample on GitHub

Take a screenshot of the map.

Sample screenshot

Use case

GIS users may want to export a screenshot of a map to enable sharing as an image or printing.

How to use the sample

Pan and zoom to find an interesting location, then use the button to take a screenshot. The screenshot will be displayed. Note that there may be a small delay if the map is still rendering when you push the button.

How it works

  1. Wait for the MapView to finish rendering the Map.
  2. Call mapView.ExportImageAsync() to get the RuntimeImage.
  3. Use the RuntimeImage.ToImageSourceAsync extension method to get a copy of the image that is compatible with native image display controls.

Relevant API

  • GeoView.ExportImageAsync
  • RuntimeImage
  • RuntimeImage.ToImageSourceAsync
  • GeoView.DrawStatus
  • GeoView.DrawStatusChanged

Tags

Screenshot, print, share, image, export, capture, screen capture, shot

Sample Code

<UserControl
    x:Class="ArcGISRuntime.UWP.Samples.TakeScreenshot.TakeScreenshot"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:esriUI="using:Esri.ArcGISRuntime.UI.Controls">
    <Grid>
        <esriUI:MapView x:Name="MyMapView" />
        <Border Style="{StaticResource BorderStyle}">
            <StackPanel>
                <Button Content="Take screenshot"
                        HorizontalAlignment="Stretch"
                        Click="OnTakeScreenshotButtonClicked" />
                <Image x:Name="ScreenshotView"
                       HorizontalAlignment="Stretch"
                       Margin="0,5,0,0"
                       Visibility="Collapsed" />
            </StackPanel>
        </Border>
    </Grid>
</UserControl>
// Copyright 2018 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.

using System;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Popups;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Esri.ArcGISRuntime.UI.Controls;

namespace ArcGISRuntime.UWP.Samples.TakeScreenshot
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Take a screenshot",
        "MapView",
        "This sample demonstrates how you can take screenshot of a map. Click 'capture' button to take a screenshot of the visible area of the map. Created image is shown in the sample after creation.",
        "")]
    public partial class TakeScreenshot
    {
        public TakeScreenshot()
        {
            InitializeComponent();
            Initialize();
        }

        private void Initialize()
        {
            // Show an imagery basemap.
            MyMapView.Map = new Map(Basemap.CreateImagery());
        }

        private async void OnTakeScreenshotButtonClicked(object sender, RoutedEventArgs e)
        {
            try
            {
                // Wait for rendering to finish before taking the screenshot.
                await WaitForRenderCompleteAsync(MyMapView);

                // Export the image from the map view.
                RuntimeImage image = await MyMapView.ExportImageAsync();

                // Convert the image to a displayable format.
                ImageSource exportedImage = await image.ToImageSourceAsync();

                // Set the screenshot view to the new exported image.
                ScreenshotView.Source = exportedImage;

                // Make the screenshot view visible in the UI.
                ScreenshotView.Visibility = Visibility.Visible;
            }
            catch (Exception ex)
            {
                await new MessageDialog(ex.ToString(), "Error").ShowAsync();
            }
        }

        private static Task WaitForRenderCompleteAsync(MapView mapview)
        {
            // The task completion source manages the task, including marking it as finished when the time comes.
            TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();

            // If the map is currently finished drawing, set the result immediately.
            if (mapview.DrawStatus == DrawStatus.Completed)
            {
                tcs.SetResult(null);
            }
            // Otherwise, configure a callback and a timeout to either set the result when
            // the map is finished drawing or set the result after 2000 ms.
            else
            {
                // Define a cancellation token source for 2000 ms.
                const int timeoutMs = 2000;
                var ct = new CancellationTokenSource(timeoutMs);

                // Register the callback that sets the task result after 2000 ms.
                ct.Token.Register(() =>
                    tcs.TrySetResult(null), false);


                // Define a local function that will set the task result and unregister itself when the map finishes drawing.
                void DrawCompleteHandler(object s, DrawStatusChangedEventArgs e)
                {
                    if (e.Status == DrawStatus.Completed)
                    {
                        mapview.DrawStatusChanged -= DrawCompleteHandler;
                        tcs.TrySetResult(null);
                    }
                }

                // Register the draw complete event handler.
                mapview.DrawStatusChanged += DrawCompleteHandler;
            }

            // Return the task.
            return tcs.Task;
        }
    }
}