How to create a raster stretch renderer


Creating a raster stretch renderer

Do the following steps to create a raster stretch renderer:
  1. Define the color ramp's from and to colors.
  2. Create the color ramp.
  3. Create a stretch renderer.
  4. Set the renderer properties.
  5. Set the stretch type.

    See the following code example:
[C#]
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 layer




To 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):
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