Creating a raster stretch renderer
Do the following steps to create a raster stretch renderer:
- Define the color ramp's from and to colors.
- Create the color ramp.
- Create a stretch renderer.
- Set the renderer properties.
- Set the stretch type.
See the following code example:
public IRasterRenderer StretchRenderer(ESRI.ArcGIS.Geodatabase.IRasterDataset
rasterDataset)
{
try
{
//Define the from and to colors for the color ramp.
IRgbColor fromColor = new RgbColorClass();
fromColor.Red = 255;
fromColor.Green = 0;
fromColor.Blue = 0;
IColor toColor = new RgbColorClass();
fromColor.Red = 0;
fromColor.Green = 255;
fromColor.Blue = 0;
//Create the color ramp.
IAlgorithmicColorRamp colorRamp = new AlgorithmicColorRampClass();
colorRamp.Size = 255;
colorRamp.FromColor = fromColor;
colorRamp.ToColor = toColor;
bool createColorRamp;
colorRamp.CreateRamp(out createColorRamp);
//Create a stretch renderer.
IRasterStretchColorRampRenderer stretchRenderer = new
RasterStretchColorRampRendererClass();
IRasterRenderer rasterRenderer = (IRasterRenderer)stretchRenderer;
//Set the renderer properties.
IRaster raster = rasterDataset.CreateDefaultRaster();
rasterRenderer.Raster = raster;
rasterRenderer.Update();
stretchRenderer.BandIndex = 0;
stretchRenderer.ColorRamp = colorRamp;
//Set the stretch type.
IRasterStretch stretchType = (IRasterStretch)rasterRenderer;
stretchType.StretchType =
esriRasterStretchTypesEnum.esriRasterStretch_StandardDeviations;
stretchType.StandardDeviationsParam = 2;
return rasterRenderer;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return null;
}
}
[VB.NET]
Public Function StretchRenderer(ByVal rasterDataset As ESRI.ArcGIS.GeodataMyBase.IRasterDataset) As IRasterRenderer
Try
'Define the from and to colors for the color ramp.
Dim fromColor As IRgbColor = New RgbColorClass()
fromColor.Red = 255
fromColor.Green = 0
fromColor.Blue = 0
Dim toColor As IColor = New RgbColorClass()
fromColor.Red = 0
fromColor.Green = 255
fromColor.Blue = 0
'Create the color ramp.
Dim colorRamp As IAlgorithmicColorRamp = New AlgorithmicColorRampClass()
colorRamp.Size = 255
colorRamp.FromColor = fromColor
colorRamp.ToColor = toColor
Dim createColorRamp As Boolean
colorRamp.CreateRamp( createColorRamp)
'Create a stretch renderer.
Dim stretchRenderer As IRasterStretchColorRampRenderer = New RasterStretchColorRampRendererClass()
Dim rasterRenderer As IRasterRenderer = CType(stretchRenderer, IRasterRenderer)
'Set the renderer properties.
Dim raster As IRaster = rasterDataset.CreateDefaultRaster()
rasterRenderer.Raster = raster
rasterRenderer.Update()
stretchRenderer.BandIndex = 0
stretchRenderer.ColorRamp = colorRamp
'Set the stretch type.
Dim stretchType As IRasterStretch = CType(rasterRenderer, IRasterStretch)
stretchType.StretchType = esriRasterStretchTypesEnum.esriRasterStretch_StandardDeviations
stretchType.StandardDeviationsParam = 2
Return rasterRenderer
Catch ex As Exception
System.Diagnostics.Debug.WriteLine(ex.Message)
Return Nothing
End Try
End Function
See Also:
How to create a raster layerTo use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
ESRI.ArcGIS.System (ESRI.ArcGIS.esriSystem)ESRI.ArcGIS.Display ESRI.ArcGIS.Geometry ESRI.ArcGIS.DataSourcesGDB ESRI.ArcGIS.Geodatabase ESRI.ArcGIS.DataSourcesRaster ESRI.ArcGIS.Carto
Development licensing | Deployment licensing |
---|---|
ArcGIS for Desktop Advanced | ArcGIS for Desktop Advanced |
ArcGIS for Desktop Standard | ArcGIS for Desktop Standard |
ArcGIS for Desktop Basic | ArcGIS for Desktop Basic |
Engine Developer Kit | Engine |