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


How to save raster data and set storage properties (ArcObjects .NET 10.5 SDK)

How to save raster data and set storage properties


Summary
This topic shows how to save a raster to various raster formats, how to set up storage properties, and how to control pyramid building.

In this topic


Saving raster data and setting storage properties

By using the ISaveAs interface, you can save a raster or raster dataset to various raster formats. You can also save to a geodatabase if the output workspace is a geodatabase workspace. See the following code example:
If the output is an ArcSDE geodatabase, an ArcGIS Desktop Standard or Advanced license is required.
[C#]
public void SaveAs(IRasterDataset rasterDataset, IWorkspace workspace, string
    sFormat, string sName)
{
    //Using ISaveAs2, specify the storage property for the output raster, such as tile size, 
    //compression, pyramid building, etc., for geodatabase raster, some also applies to output 
    //as a file format.
    ISaveAs saveAs=rasterDataset as ISaveAs;
    saveAs.SaveAs(sName, workspace, sFormat);
}


public void SaveAsWithCompressionAndTile(IRasterDataset rasterDataset)
{
    //rasterDataset can be created from any data source, file system, or geodatabase. 
    //It can also be a raster object (IRaster).
    ISaveAs2 saveAs2=(ISaveAs2)rasterDataset;

    //Set storage. 
    IRasterStorageDef rasterStorageDef=new RasterStorageDefClass();
    IRasterStorageDef2 rasterStorageDef2=(IRasterStorageDef2)rasterStorageDef;
    rasterStorageDef2.CompressionType =
        esriRasterCompressionType.esriRasterCompressionJPEG2000;
    rasterStorageDef2.CompressionQuality=50;
    rasterStorageDef2.Tiled=true;
    rasterStorageDef2.TileHeight=128;
    rasterStorageDef2.TileWidth=128;

    //Set output workspace.
    IRasterWorkspaceEx rasterWorkspaceEx=OpenFileGDBWorkspace("c:\\temp\\fgdb.gdb")
        ;
    IWorkspace workspace=(IWorkspace)rasterWorkspaceEx;
    saveAs2.SaveAsRasterDataset("filegdbRaster", workspace, "gdb", rasterStorageDef2)
        ;
}


public IRasterWorkspaceEx OpenFileGDBWorkspace(string sPath)
{
    IWorkspaceFactory workspaceFactory=new FileGDBWorkspaceFactoryClass();
    IWorkspace workspace=workspaceFactory.OpenFromFile(sPath, 0);
    IRasterWorkspaceEx rasterWorkspaceEx=(IRasterWorkspaceEx)workspace;

    return rasterWorkspaceEx;
}
[VB.NET]
Public Sub SaveAs(ByVal rasterDataset As IRasterDataset, ByVal workspace As IWorkspace, ByVal sFormat As String, ByVal sName As String)
    
    'Using ISaveAs2, specify the storage property for the output raster, such as tile size, compression,
    'and pyramid building, etc., for geodatabase raster, some also applies to output as a file format.
    Dim saveAs As ISaveAs=TryCast(rasterDataset, ISaveAs)
    saveAs.SaveAs(sName, workspace, sFormat)
    
End Sub


Public Sub SaveAsWithCompressionAndTile(ByVal rasterDataset As IRasterDataset)
    
    'rasterDataset can be created from any data source, file system, or geodatabase.
    'It can also be a raster object (IRaster).
    Dim saveAs2 As ISaveAs2=CType(rasterDataset, ISaveAs2)
    
    'Set storage.
    Dim rasterStorageDef As IRasterStorageDef=New RasterStorageDefClass
    Dim rasterStorageDef2 As IRasterStorageDef2=CType(rasterStorageDef, IRasterStorageDef2)
    
    rasterStorageDef2.CompressionType=esriRasterCompressionType.esriRasterCompressionJPEG2000
    rasterStorageDef2.CompressionQuality=50
    rasterStorageDef2.Tiled=True
    rasterStorageDef2.TileHeight=128
    rasterStorageDef2.TileWidth=128
    
    'Set output workspace.
    Dim rasterWorkspaceEx As IRasterWorkspaceEx=OpenFileGDBWorkspace("c:\temp\fgdb.gdb")
    Dim workspace As IWorkspace=CType(rasterWorkspaceEx, IWorkspace)
    
    saveAs2.SaveAsRasterDataset("filegdbRaster", workspace, "gdb", rasterStorageDef2)
    
End Sub


Public Function OpenFileGDBWorkspace(ByVal sPath As String) As IRasterWorkspaceEx
    
    Dim workspaceFactory As IWorkspaceFactory=New FileGDBWorkspaceFactoryClass
    Dim workspace As IWorkspace=workspaceFactory.OpenFromFile(sPath, 0)
    Dim rasterWorkspaceEx As IRasterWorkspaceEx=CType(workspace, IRasterWorkspaceEx)
    
    Return rasterWorkspaceEx
    
End Function

Controlling pyramid building

You can also turn off pyramid building for outputs—such as IMG, GRID, and TIFF formats—using the ISaveAs2 interface. See the following code example:
[C#]
public void SaveAsWithoutBuildingPyramids(IRaster raster)
{
    ISaveAs2 saveAs2=(ISaveAs2)raster;

    IRasterStorageDef rasterStorageDef=new RasterStorageDefClass();
    IRasterStorageDef2 rasterStorageDef2=(IRasterStorageDef2)rasterStorageDef;

    rasterStorageDef2.PyramidLevel=0;

    IRasterWorkspace rasterWorkspace=SetRasterWorkspace("c:\\temp");
    IWorkspace workspace=(IWorkspace)rasterWorkspace;

    saveAs2.SaveAsRasterDataset("image1.img", workspace, "IMAGINE Image",
        rasterStorageDef2);

}


public IRasterWorkspace SetRasterWorkspace(string sPath)
{
    IWorkspaceFactory workspaceFactory=new RasterWorkspaceFactoryClass();
    IWorkspace workspace=workspaceFactory.OpenFromFile(sPath, 0);
    IRasterWorkspace rasterWorkspace=(IRasterWorkspace)workspace;

    return rasterWorkspace;
}
[VB.NET]
Public Sub SaveAsWithoutBuildingPyramids(ByVal raster As IRaster)
    
    Dim saveAs2 As ISaveAs2=CType(raster, ISaveAs2)
    
    Dim rasterStorageDef As IRasterStorageDef=New RasterStorageDefClass
    Dim rasterStorageDef2 As IRasterStorageDef2=CType(rasterStorageDef, IRasterStorageDef2)
    
    rasterStorageDef2.PyramidLevel=0
    
    Dim rasterWorkspace As IRasterWorkspace=SetRasterWorkspace("c:\temp")
    Dim workspace As IWorkspace=CType(rasterWorkspace, IWorkspace)
    
    saveAs2.SaveAsRasterDataset("image1.img", workspace, "IMAGINE Image", rasterStorageDef2)
    
End Sub


Public Function SetRasterWorkspace(ByVal sPath As String) As IRasterWorkspace
    
    Dim workspaceFactory As IWorkspaceFactory=New RasterWorkspaceFactoryClass
    Dim workspace As IWorkspace=workspaceFactory.OpenFromFile(sPath, 0)
    Dim rasterWorkspace As IRasterWorkspace=CType(workspace, IRasterWorkspace)
    
    Return rasterWorkspace
    
End Function






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