How to use a raster stretch renderer


This sample shows the basics of using RasterStretchColorRampRenderer.

How to use

  1. Add a raster layer that has a table into to ArcMap and make sure it is first layer.
  2. Run the procedure in ArcMap Visual Basic Editor.
[VBA]
Sub UsingRasterStretchColorRampRender()
    
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Dim pMap As IMap
    Set pMap = pMxDoc.FocusMap
    
    'Get raster input from layer
    Dim pRLayer As IRasterLayer
    Set pRLayer = pMap.Layer(0)
    Dim pRaster As IRaster
    Set pRaster = pRLayer.Raster
    
    'Create renderer and QI RasterRenderer
    Dim pStretchRen As IRasterStretchColorRampRenderer
    Set pStretchRen = New RasterStretchColorRampRenderer
    Dim pRasRen As IRasterRenderer
    Set pRasRen = pStretchRen
    
    'Set raster for the renderer and update
    Set pRasRen.Raster = pRaster
    pRasRen.Update
    
    'Define two colors
    Dim pFromColor As IColor
    Dim pToColor As IColor
    Set pFromColor = New RgbColor
    pFromColor.RGB = RGB(255, 0, 0)
    Set pToColor = New RgbColor
    pToColor.RGB = RGB(0, 255, 0)
    
    'Create color ramp
    Dim pRamp As IAlgorithmicColorRamp
    Set pRamp = New AlgorithmicColorRamp
    pRamp.Size = 255
    pRamp.FromColor = pFromColor
    pRamp.ToColor = pToColor
    pRamp.CreateRamp True
    
    'Plug this colorramp into renderer and select a band
    pStretchRen.BandIndex = 0
    pStretchRen.ColorRamp = pRamp
    
    'Update the renderer with new settings and plug into layer
    pRasRen.Update
    Set pRLayer.Renderer = pStretchRen
    pMxDoc.ActiveView.Refresh
    pMxDoc.UpdateContents
    
    'Release memeory
    Set pMxDoc = Nothing
    Set pRLayer = Nothing
    Set pRaster = Nothing
    Set pStretchRen = Nothing
    Set pRasRen = Nothing
    Set pRamp = Nothing
    Set pToColor = Nothing
    Set pFromColor = Nothing
    
End Sub