/// <summary>
        /// Set the stretch type on the first selected image service layer in the first open 2D map.
        /// </summary>
        /// <param name="stretschType">The stretch type to set on the layer.</param>
        /// <param name="statsType">The stretch statistics type to set on the layer. This lets you pick between Dataset, Area of View (to enable DRA) or Custom statistics.</param>
        /// <returns></returns>
        public static async Task SetStretchTypeAsync(RasterStretchType stretschType, RasterStretchStatsType statsType = RasterStretchStatsType.Dataset)
        {
            try
            {
                // Get the first 2D map from the project that is called Map.
                Map _map = await GetMapFromProject(Project.Current, "Map");

                if (_map == null)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("SetStretchType: Failed to get map.");
                    return;
                }

                // Get the most recently selected layer of type ImageServiceLayer
                ImageServiceLayer imageServiceLayer = MapView.Active.GetSelectedLayers().OfType <ImageServiceLayer>().FirstOrDefault();
                if (imageServiceLayer != null)
                {
                    // Set the colorizer on the most recently selected layer.
                    // The colorizer has to be set on the Main CIM Thread (MCT).
                    await QueuedTask.Run(() =>
                    {
                        // Get the colorizer from the selected layer.
                        CIMRasterColorizer newColorizer = imageServiceLayer.GetColorizer();
                        // Set the stretch type and stretch statistics type on the colorizer.
                        // Theese parameters only apply to the Stretch and RGB colorizers.
                        if (newColorizer is CIMRasterRGBColorizer)
                        {
                            ((CIMRasterRGBColorizer)newColorizer).StretchType      = stretschType;
                            ((CIMRasterRGBColorizer)newColorizer).StretchStatsType = statsType;
                        }
                        else if (newColorizer is CIMRasterStretchColorizer)
                        {
                            ((CIMRasterStretchColorizer)newColorizer).StretchType = stretschType;
                            ((CIMRasterStretchColorizer)newColorizer).StatsType   = statsType;
                        }
                        else
                        {
                            MessageBox.Show("Selected layer must be visualized using the RGB or Stretch colorizer");
                        }
                        // Update the image service with the new colorizer
                        imageServiceLayer.SetColorizer(newColorizer);
                    });
                }
            }
            catch (Exception exc)
            {
                // Catch any exception found and display a message box.
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Exception caught while trying to set stretch type: " + exc.Message);
                return;
            }
        }
        /// <summary>
        /// Set the resampling type on the first selected image service layer in the first open 2D map.
        /// </summary>
        /// <param name="resamplingType">The resampling type to set on the layer.</param>
        /// <returns></returns>
        public static async Task SetResamplingTypeAsync(RasterResamplingType resamplingType)
        {
            try
            {
                // Get the first 2D map from the project that is called Map.
                Map _map = await GetMapFromProject(Project.Current, "Map");

                if (_map == null)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("SetResamplingType: Failed to get map.");
                    return;
                }

                // Get the most recently selected layer of type ImageServiceLayer
                ImageServiceLayer imageServiceLayer = MapView.Active.GetSelectedLayers().OfType <ImageServiceLayer>().FirstOrDefault();
                if (imageServiceLayer != null)
                {
                    // Set the colorizer on the most recently selected layer.
                    // The colorizer has to be get/set on the Main CIM Thread (MCT).
                    await QueuedTask.Run(() =>
                    {
                        // Get the colorizer from the selected layer.
                        CIMRasterColorizer newColorizer = imageServiceLayer.GetColorizer();
                        // Set the resampling type on the colorizer.
                        newColorizer.ResamplingType = resamplingType;
                        // Update the image service with the new colorizer
                        imageServiceLayer.SetColorizer(newColorizer);
                    });
                }
            }
            catch (Exception exc)
            {
                // Catch any exception found and display a message box.
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Exception caught while trying to set resampling type: " + exc.Message);
            }
        }