How to save and load color maps


Saving and loading color maps

Do the following steps to load a color map to a dataset:
  1. Cast the RasterDataset object into a RasterDatasetEdit object.
  2. Create a RasterColormap3 object.
  3. Load the .clr color map file.
  4. Alter the color map through the RasterDatasetEdit object.
Do the following steps to save a color map from a dataset:
  1. Cast the RasterDataset object into a RasterBandCollection object.
  2. Load a single raster band from the RasterBandCollection into a RasterBand object.
  3. Load the RasterBand.Colormap into a RasterColormap3 object.
  4. Call the RasterColormap3.SaveTo function to save the .clr file.

See the following code example:
[C#]
// Load a color map from a .clr file to a raster dataset.
// .clr string example: "C:\colormap\MyColormap.clr"
public static void LoadColormap(IRasterDataset rasDs, string clrPathAndName)
{
    // Use the IRasterDatasetEdit interface to change the dataset color map.
    IRasterDatasetEdit rasEdit = (IRasterDatasetEdit)rasDs;
    // Create a color map.
    IRasterColormap3 rasColor3 = new RasterColormapClass();
    // Load the color map from the .clr file.
    rasColor3.LoadFrom(clrPathAndName);
    // Change the color map of the dataset. 
    rasEdit.AlterColormap((IRasterColormap)rasColor3);
}

// Save a color map from a dataset to a .clr file.
// .clr string example: "C:\colormap\MyNewColormap.clr"
public static void SaveColormap(IRasterDataset rasDs, string clrPathAndName)
{
    // Get the color map band of the dataset.
    IRasterBandCollection rasBC = (IRasterBandCollection)rasDs;
    IRasterBand rasBand = rasBC.Item(0);
    // Query interface (QI) to the IRasterColormap3 interface and save to a .clr file.
    IRasterColormap3 rasColor3 = (IRasterColormap3)rasBand.Colormap;
    rasColor3.SaveTo(clrPathAndName);
}
[VB.NET]
' Load a color map from a .clr file to a raster dataset.
' .clr string example: "C:\colormap\MyColormap.clr"

Public Sub LoadColormap(ByVal rasDs As IRasterDataset, ByVal clrPathAndName As String)
    ' Use the IRasterDatasetEdit interface to change the dataset color map.
    Dim rasEdit As IRasterDatasetEdit
    rasEdit = rasDs
    ' Create a color map.
    Dim rasColor3 As IRasterColormap3
    rasColor3 = New RasterColormapClass
    ' Load the color map from the .clr file.
    rasColor3.LoadFrom(clrPathAndName)
    ' Change the color map of the dataset.
    rasEdit.AlterColormap(rasColor3)
End Sub

' Save a color map from a dataset to a .clr file.
' .clr string example: "C:\colormap\MyNewColormap.clr"

Public Sub SaveColormap(ByVal rasDs As IRasterDataset, ByVal clrPathAndName As String)
    ' Get the color map band from the dataset.
    Dim rasBC As IRasterBandCollection
    rasBC = rasDs
    Dim rasBand As IRasterBand
    rasBand = rasBC.Item(0)
    ' Query interface (QI) to the IRasterColormap3 interface and save to a .clr file.
    Dim rasColor3 As IRasterColormap3
    rasColor3 = rasBand.Colormap
    rasColor3.SaveTo(clrPathAndName)
End Sub






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