Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

RGB Renderer

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

Use an RGBRenderer on a RasterLayer. An RGBRenderer is used to adjust the color bands of a multispectral image.

screenshot

How to use the sample

Configure the options for rendering, then click 'Apply hillshade'.

How it works

  1. Create a Raster from a from a multispectral raster file.
  2. Create a RasterLayer from the Raster.
  3. Create a Basemap from the RasterLayer and set it to the map.
  4. Create a RGBRenderer, specifying the stretch parameters and other properties.
  5. Apply the renderer to the raster layer.

Relevant API

  • Map
  • Basemap
  • MapView
  • RGBRenderer
  • Raster
  • RasterLayer
  • StretchParameters

Offline data

This sample downloads the following items from ArcGIS Online automatically:

Tags

RGB, Visualization, analysis, bands, imagery, multispectral, raster, renderer

Sample Code

<UserControl x:Class="ArcGISRuntime.UWP.Samples.RasterRgbRenderer.RasterRgbRenderer"
             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">
    <UserControl.Resources>
        <Style TargetType="ComboBox">
            <Setter Property="HorizontalAlignment" Value="Stretch" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="Margin" Value="5,5,0,0" />
        </Style>
        <Style TargetType="TextBlock">
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="HorizontalAlignment" Value="Right" />
            <Setter Property="FontWeight" Value="SemiBold" />
        </Style>
        <Style TargetType="Slider">
            <Setter Property="Minimum" Value="0" />
            <Setter Property="Maximum" Value="100" />
            <Setter Property="Padding" Value="5,8,0,0" />
            <Setter Property="VerticalAlignment" Value="Center" />
        </Style>
    </UserControl.Resources>
    <Grid>
        <esriUI:MapView x:Name="MyMapView" />
        <Border  Style="{StaticResource BorderStyle}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" MinWidth="100" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="Stretch type: "
                           Grid.Row="0" Grid.Column="0"
                           HorizontalAlignment="Right" VerticalAlignment="Center" />
                <ComboBox x:Name="StretchTypeComboBox"
                          Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="3"
                          SelectionChanged="StretchTypeComboBox_SelectionChanged" />
                <Grid x:Name="MinMaxParametersGrid"
                      Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4"
                      Margin="0,0,0,5"
                      Visibility="Visible">
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" MinWidth="100" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Row="0" Grid.Column="0"
                               Text="Min value: "
                               HorizontalAlignment="Right" VerticalAlignment="Center" />
                    <ComboBox x:Name="MinRedComboBox"
                              Grid.Row="0" Grid.Column="1"
                              Foreground="Red" />
                    <ComboBox x:Name="MinGreenComboBox"
                              Grid.Row="0" Grid.Column="2"
                              Foreground="Green" />
                    <ComboBox x:Name="MinBlueComboBox"
                              Grid.Row="0" Grid.Column="3"
                              Foreground="Blue" />
                    <TextBlock Grid.Row="1" Grid.Column="0"
                               Text="Max value: " />
                    <ComboBox x:Name="MaxRedComboBox"
                              Grid.Row="1" Grid.Column="1"
                              Foreground="Red" />
                    <ComboBox x:Name="MaxGreenComboBox"
                              Grid.Row="1" Grid.Column="2"
                              Foreground="Green" />
                    <ComboBox x:Name="MaxBlueComboBox"
                              Grid.Row="1" Grid.Column="3"
                              Foreground="Blue" />
                </Grid>
                <Grid x:Name="PercentClipParametersGrid"
                      Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4"
                      Visibility="Collapsed">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" MinWidth="100" />
                        <ColumnDefinition Width="3*" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Row="0" Grid.Column="0"
                               Text="Minimum %:" />
                    <Slider x:Name="MinimumValueSlider"
                            Grid.Row="0" Grid.Column="1" />
                    <TextBlock Grid.Row="1" Grid.Column="0"
                               Text="Maximum %: " />
                    <Slider x:Name="MaximumValueSlider"
                            Grid.Row="1" Grid.Column="1" />
                </Grid>
                <Grid x:Name="StdDeviationParametersGrid"
                      Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4"
                      Visibility="Collapsed">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" MinWidth="100" />
                        <ColumnDefinition Width="3*" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="Factor: "
                               Grid.Column="0" />
                    <ComboBox x:Name="StdDeviationFactorComboBox"
                              Margin="5,5,0,5"
                              Grid.Column="1" />
                </Grid>
                <Button x:Name="ApplyRgbRendererButton"
                        Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="4"
                        HorizontalAlignment="Stretch"
                        IsEnabled="False"
                        Content="Apply renderer"
                        Click="ApplyRgbRendererButton_Click" />
            </Grid>
        </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 ArcGISRuntime.Samples.Managers;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Rasters;
using System;
using System.Collections.Generic;
using System.Linq;
using Windows.UI.Popups;

namespace ArcGISRuntime.UWP.Samples.RasterRgbRenderer
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Raster RGB renderer",
        "Layers",
        "This sample demonstrates how to use an RGB renderer on a raster layer. An RGB renderer is used to adjust the color bands of a multi-spectral image.",
        "Choose one of the stretch parameter types. The other options will adjust based on the chosen type. Add your inputs and press the Apply button to update the renderer.")]
	[ArcGISRuntime.Samples.Shared.Attributes.OfflineData("7c4c679ab06a4df19dc497f577f111bd")]
    public partial class RasterRgbRenderer
    {
        // A reference to the raster layer to render.
        private RasterLayer _rasterLayer;

        public RasterRgbRenderer()
        {
            InitializeComponent();

            // Call a function to create the map, add a raster layer, and fill UI controls.
            Initialize();
        }

        private async void Initialize()
        {
            // Create a map with a streets basemap.
            Map map = new Map(Basemap.CreateStreets());

            // Get the file name for the local raster dataset.
            string filepath = GetRasterPath();

            // Load the raster file
            Raster rasterFile = new Raster(filepath);

            // Create a new raster layer to show the image.
            _rasterLayer = new RasterLayer(rasterFile);

            try
            {
                // Once the layer is loaded, enable the button to change the renderer.
                await _rasterLayer.LoadAsync();
                ApplyRgbRendererButton.IsEnabled = true;

                // Create a viewpoint with the raster's full extent.
                Viewpoint fullRasterExtent = new Viewpoint(_rasterLayer.FullExtent);

                // Set the initial viewpoint for the map.
                map.InitialViewpoint = fullRasterExtent;

                // Add the layer to the map.
                map.OperationalLayers.Add(_rasterLayer);

                // Add the map to the map view.
                MyMapView.Map = map;

                // Add available stretch types to the combo box.
                StretchTypeComboBox.Items.Add("Min Max");
                StretchTypeComboBox.Items.Add("Percent Clip");
                StretchTypeComboBox.Items.Add("Standard Deviation");

                // Select "Min Max" as the stretch type.
                StretchTypeComboBox.SelectedIndex = 0;

                // Create a range of values from 0-255.
                IEnumerable<int> minMaxValues = Enumerable.Range(0, 256);

                // Fill the min and max red combo boxes with the range and set default values.
                MinRedComboBox.ItemsSource = minMaxValues;
                MinRedComboBox.SelectedValue = 0;
                MaxRedComboBox.ItemsSource = minMaxValues;
                MaxRedComboBox.SelectedValue = 255;

                // Fill the min and max green combo boxes with the range and set default values.
                MinGreenComboBox.ItemsSource = minMaxValues;
                MinGreenComboBox.SelectedValue = 0;
                MaxGreenComboBox.ItemsSource = minMaxValues;
                MaxGreenComboBox.SelectedValue = 255;

                // Fill the min and max blue combo boxes with the range and set default values.
                MinBlueComboBox.ItemsSource = minMaxValues;
                MinBlueComboBox.SelectedValue = 0;
                MaxBlueComboBox.ItemsSource = minMaxValues;
                MaxBlueComboBox.SelectedValue = 255;

                // Fill the standard deviation factor combo box and set a default value.
                IEnumerable<int> wholeStdDevs = Enumerable.Range(1, 10);
                List<double> halfStdDevs = wholeStdDevs.Select(i => (double)i / 2).ToList();
                StdDeviationFactorComboBox.ItemsSource = halfStdDevs;
                StdDeviationFactorComboBox.SelectedValue = 2.0;
            }
            catch (Exception e)
            {
                await new MessageDialog(e.ToString(), "Error").ShowAsync();
            }
        }

        private void ApplyRgbRendererButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            // Create the correct type of StretchParameters with the corresponding user inputs.
            StretchParameters stretchParameters = null;

            // See which type is selected and apply the corresponding input parameters to create the renderer.
            switch (StretchTypeComboBox.SelectedValue.ToString())
            {
                case "Min Max":
                    // Read the minimum and maximum values for the red, green, and blue bands.
                    double minRed = Convert.ToDouble(MinRedComboBox.SelectedValue);
                    double minGreen = Convert.ToDouble(MinGreenComboBox.SelectedValue);
                    double minBlue = Convert.ToDouble(MinBlueComboBox.SelectedValue);
                    double maxRed = Convert.ToDouble(MaxRedComboBox.SelectedValue);
                    double maxGreen = Convert.ToDouble(MaxGreenComboBox.SelectedValue);
                    double maxBlue = Convert.ToDouble(MaxBlueComboBox.SelectedValue);

                    // Create an array of the minimum and maximum values.
                    double[] minValues = { minRed, minGreen, minBlue };
                    double[] maxValues = { maxRed, maxGreen, maxBlue };

                    // Create a new MinMaxStretchParameters with the values.
                    stretchParameters = new MinMaxStretchParameters(minValues, maxValues);
                    break;
                case "Percent Clip":
                    // Get the percentile cutoff below which values in the raster dataset are to be clipped.
                    double minimumPercent = MinimumValueSlider.Value;

                    // Get the percentile cutoff above which pixel values in the raster dataset are to be clipped.
                    double maximumPercent = MaximumValueSlider.Value;

                    // Create a new PercentClipStretchParameters with the inputs.
                    stretchParameters = new PercentClipStretchParameters(minimumPercent, maximumPercent);
                    break;
                case "Standard Deviation":
                    // Read the standard deviation factor (the number of standard deviations used to define the range of pixel values).
                    double standardDeviationFactor = Convert.ToDouble(StdDeviationFactorComboBox.SelectedValue);

                    // Create a new StandardDeviationStretchParameters with the selected number of standard deviations.
                    stretchParameters = new StandardDeviationStretchParameters(standardDeviationFactor);
                    break;
            }

            // Create an array to specify the raster bands (red, green, blue).
            int[] bands = { 0, 1, 2 };

            // Create the RgbRenderer with the stretch parameters created above, then apply it to the raster layer.
            RgbRenderer rasterRenderer = new RgbRenderer(stretchParameters, bands, null, true);
            _rasterLayer.Renderer = rasterRenderer;
        }

        private void StretchTypeComboBox_SelectionChanged(object sender, Windows.UI.Xaml.Controls.SelectionChangedEventArgs e)
        {
            // Hide all UI controls for the input parameters.
            MinMaxParametersGrid.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            PercentClipParametersGrid.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            StdDeviationParametersGrid.Visibility = Windows.UI.Xaml.Visibility.Collapsed;

            // See which type was selected and show the corresponding input controls.
            switch (StretchTypeComboBox.SelectedValue.ToString())
            {
                case "Min Max":
                    MinMaxParametersGrid.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    break;
                case "Percent Clip":
                    PercentClipParametersGrid.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    break;
                case "Standard Deviation":
                    StdDeviationParametersGrid.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    break;
            }
        }

        private static string GetRasterPath()
        {
            return DataManager.GetDataFolder("7c4c679ab06a4df19dc497f577f111bd", "raster-file", "Shasta.tif");
        }
    }
}