Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Stretch

Download Samples Repository

Description

This sample demonstrates the Stretch raster function.

"Desktop" "Store" "Phone" Available for Desktop, Store, Phone

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.Stretch"
		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
		xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013">
	<Grid>
		<esri:MapView x:Name="MyMapView">
			<esri:Map >
				<esri:ArcGISImageServiceLayer ID="ImageServiceLayer" 
										  ServiceUri="http://sampleserver6.arcgisonline.com/arcgis/rest/services/Toronto/ImageServer" 
										  ImageFormat="PNG8"
										  NoData="0"/>
				<esri:ArcGISTiledMapServiceLayer Opacity="100" 
					ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/Reference/World_Transportation/MapServer" />
			</esri:Map>
		</esri:MapView>

		<Border Background="White" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Right" VerticalAlignment="Top"
						Margin="30,20,20,30" Padding="10">
			<StackPanel Orientation="Vertical">
				<TextBlock x:Name="ResponseTextBlock" 
						   Text="Define input properties for the stretch raster function and click Apply." 
						   Width="200" TextAlignment="Left" 
						   TextWrapping="Wrap" Margin="0,0,0,5"/>
				<StackPanel Orientation="Vertical" Margin="10">
					<RadioButton x:Name="NoneCheckBox" Content="None" GroupName="StretchType" IsChecked="True"  Margin="3,0,0,0"/>
					<RadioButton x:Name="StandardDevCheckBox" Content="Standard Deviation" GroupName="StretchType"  Margin="3,0,0,0"/>
					<RadioButton x:Name="HistogramCheckBox" Content="Histogram" GroupName="StretchType" Margin="3,0,0,0" />
					<RadioButton x:Name="MinMaxCheckBox" Content="MinMax" GroupName="StretchType"  Margin="3,0,0,0"/>
					<StackPanel Orientation="Horizontal" Margin="3">
						<TextBlock Text="Standard Deviations" VerticalAlignment="Center" Margin="5"  />
						<TextBox x:Name="StnDevTextBox" Text="2" />
					</StackPanel>
					<StackPanel Orientation="Horizontal" Margin="3">
						<TextBlock Text="Band IDs" Margin="5" VerticalAlignment="Center" />
						<TextBox x:Name="BandIDsTextBox" Text="3,1,2" />
					</StackPanel>
					<TextBlock x:Name="ValidBandIdsTextBlock" Text="3 bands must be defined" Foreground="Red" 
									   Visibility="Collapsed" />
				</StackPanel>
				<Button Content="Apply" Margin="10" Click="ApplyButton_Click" />
			</StackPanel>
		</Border>
	</Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
	/// <summary>
	/// This sample demonstrates the Stretch raster function. 
	/// </summary>
	/// <title>Stretch</title>
	/// <category>Layers</category>
	/// <subcategory>Dynamic Service Layers</subcategory>
	public partial class Stretch : UserControl
	{
		public Stretch()
		{
			InitializeComponent();
			MyMapView.Map.InitialViewpoint = 
				new Viewpoint(new Envelope(-8844874, 5401062, -8828990, 5420947, SpatialReference.Create(3857)));

		}

		private void ApplyButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			try
			{
				ArcGISImageServiceLayer imageLayer = MyMapView.Map.Layers["ImageServiceLayer"] as ArcGISImageServiceLayer;
				RenderingRule renderingRule = new RenderingRule();
				renderingRule.VariableName = "Raster";

				Dictionary<string, object> rasterParams = new Dictionary<string, object>();

				string[] strArray = BandIDsTextBox.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

				if (strArray.Length == 1 || strArray.Length == 2 || strArray.Length > 3)
				{
					ValidBandIdsTextBlock.Visibility = Visibility.Visible;
					return;
				}
				else
					ValidBandIdsTextBlock.Visibility = Visibility.Collapsed;

				renderingRule.RasterFunctionName = "Stretch";
				renderingRule.VariableName = "Raster";

				int stretchType = 0;
				if (StandardDevCheckBox.IsChecked.Value)
					stretchType = 3;
				else if (HistogramCheckBox.IsChecked.Value)
					stretchType = 4;
				else if (MinMaxCheckBox.IsChecked.Value)
					stretchType = 5;

				rasterParams.Add("StretchType", stretchType);
				rasterParams.Add("NumberOfStandardDeviations",
					string.IsNullOrEmpty(StnDevTextBox.Text) ? 1 : double.Parse(StnDevTextBox.Text));

				double[][] statistics = new double[3][] { 
					new double[4] { 0.2, 222.46, 99.35, 1.64 }, 
					new double[4] { 5.56, 100.345, 45.4, 3.96 }, 
					new double[4] { 0, 352.37, 172.284, 2 } };
				rasterParams.Add("Statistics", statistics);

				double[] gamma = new double[] { 1.25, 2, 3.95 };
				rasterParams.Add("Gamma", gamma);


				int[] numArray = new int[strArray.Length];
				for (int i = 0; i < strArray.Length; i++)
				{
					numArray[i] = int.Parse(strArray[i]);
				}

				imageLayer.BandIds = numArray.Length < 1 ? null : numArray;


				renderingRule.RasterFunctionArguments = rasterParams;
				imageLayer.RenderingRule = renderingRule;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "Sample Error");
			}
		}
	}
}
Feedback on this topic?