How to set the background color to RGB


This sample shows how to set background color when display raster with RGB renderer .

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 UsingRasterClassifyColorRampRenderer()
    ' Get Map

    Sub SetRasterBackgroundValueToRGB()
        
        Dim pMxDoc As IMxDocument
        Set pMxDoc = ThisDocument
        Dim pMap As IMap
        Set pMap = pMxDoc.FocusMap
        
        'Get raster input from layer
        Dim pRasterLayer As IRasterLayer
        Set pRasterLayer = pMap.Layer(0)
        
        'Get RGB renderer from layer and QI IRasterStretch2
        Dim pRasterRenderer As IRasterRenderer
        Set pRasterRenderer = pRasterLayer.Renderer
        If Not pRasterRenderer Is IRasterRGBRenderer Then Exit Sub
        Dim pRasterStretch As IRasterStretch2
        Set pRasterStretch = pRasterRenderer
        
        'Define the background color
        Dim myColor As IColor
        Set myColor = New RgbColor
        myColor.RGB = RGB(0, 255, 0) 'Green
        
        'Define the background pixels
        Dim PixelValue As Variant
        Dim Values(0 To 2) As Integer
        Values(0) = 76
        Values(1) = 92
        Values(2) = 114
        PixelValue = Values
        
        With pRasterStretch
            .Background = True
            .BackgroundValue = Values
            Set .BackgroundColor = myColor
        End With
        
        pRasterRenderer.Update
        pMxDoc.ActiveView.Refresh
        pMxDoc.UpdateContents
    End Sub