Using RasterStretchColorRampRenderer


This sample shows the basic steps of using RasterStretchColorRampRenderer.

How to use:
  1. Make sure the first layer of ArcMap is raster layer.
  2. Run the procedure in ArcMap Visual Basic Editor. The layer will be rendered accordingly.
Sub UsingRasterStretchColorRampRender()
     ' Get Map
     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

     ' 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
    Set pRLayer.Renderer = pStretchRen
     '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