This document is archived and information here might be outdated.  Recommended version.


How to add or remove a color map from a raster dataset (ArcObjects .NET 10.4 SDK)

How to add or remove a color map from a raster dataset


Adding or removing a color map from a raster dataset

This topic shows how to add or remove a color map from a raster dataset. The raster dataset has to be a single-band dataset to apply a color map. Raster color maps can be removed from the raster dataset using the Remove Colormap Geoprocessing tool.
Do the following steps to programmatically add or remove color maps:
  1. Cast to IRasterDatasetEdit for the color map operation.
  2. To add a color map, get the color map from the color map raster dataset through the raster band object and use the AlterColormap method on IRasterDatasetEdit.
  3. To remove a color map, call the DeleteColormap method on IRasterDatasetEdit.

    See the following code example:
[C#]
static void AddRemoveColormap(IRasterDataset rasterDataset, IRasterDataset
    colormapDataset)
{
    //If the colormapDataset is null, it will remove the color map.
    //If the colormapDataset is not null, it will add the color map of the colormapDataset to the input raster.

    //Cast to IRasterDatasetEdit for the color map operation.
    IRasterDatasetEdit rasterDatasetEdit=(IRasterDatasetEdit)rasterDataset;

    //Add a color map from a given raster dataset.
    if (colormapDataset != null)
    {
        //Get the raster color map from the given raster dataset.
        IRasterBandCollection rasterBandCollection=(IRasterBandCollection)
            colormapDataset;
        IRasterBand rasterBand=rasterBandCollection.Item(0);
        IRasterColormap rasterColormap=rasterBand.Colormap;
        if (rasterColormap != null)
        {
            rasterDatasetEdit.AlterColormap(rasterColormap);
        }
    }
    else
    {
        //Remove the raster color map.
        rasterDatasetEdit.DeleteColormap();
    }
}
[VB.NET]
Public Sub AddRemoveColormap(ByVal rasterDataset As IRasterDataset, ByVal colormapDataset As IRasterDataset)
    'If the colormapDataset is null, it will remove the color map.
    'If the colormapDataset is not null, it will add the color map of the colormapDataset to the input raster.
    
    'Cast to IRasterDatasetEdit for the color map operation.
    Dim rasterDatasetEdit As IRasterDatasetEdit=CType(rasterDataset, IRasterDatasetEdit)
    
    'Add a color map from a given raster dataset.
    If Not colormapDataset Is Nothing Then
        'Get the raster color map from the given raster dataset.
        Dim rasterBandCollection As IRasterBandCollection=CType(colormapDataset, IRasterBandCollection)
        Dim rasterBand As IRasterBand=rasterBandCollection.Item(0)
        Dim rasterColormap As IRasterColormap=rasterBand.Colormap
        If Not rasterColormap Is Nothing Then
            rasterDatasetEdit.AlterColormap(rasterColormap)
        End If
    Else
        'Remove the raster color map.
        rasterDatasetEdit.DeleteColormap()
    End If
End Sub


See Also:

How to access a raster dataset




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 Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine