How to set raster properties during SaveAs a raster

This document shows how to set raster properties on a raster before using the Save as function, including how to set the cell size and how to set the NoData value.

Setting raster properties when saving as a raster

ISaveAs can work with both a raster dataset and raster. If used on a raster dataset, the Save as function basically performs a format conversion. If used on a raster, the property of the raster can be modified.
To set a raster with a specific cell size, the row and column need to be adjusted. See the following:
static void setCellSizeAndExtentOnRaster(double cellSize, IEnvelope extent, IRaster
    raster)throws Exception{
    //Clip a raster or transform a raster; the cell size will be changed. To set a specified cell size for the output, you need to adjust the
    //extent, row, and column accordingly.

    //The cellSize is the output cell size of the raster.
    //The extent is the output extent of the raster.
    //The raster is the input raster.

    //Calculate the height and width of the output raster.
    int col = (int)((extent.getXMax() - extent.getXMin()) / cellSize);
    int row = (int)((extent.getYMax() - extent.getYMin()) / cellSize);
    //Adjust the extent.
    IPoint lowerLeft = extent.getLowerLeft();
    IPoint UpperRight = new Point();
    IEnvelope newExtent = new Envelope();
    UpperRight.setX(lowerLeft.getX() + col * cellSize);
    UpperRight.setY(lowerLeft.getY() + row * cellSize);

    //Then put the extent, height, and width.
    IRasterProps rasterProps = new IRasterPropsProxy(raster);

    //Save it.
    ISaveAs saveAs = new ISaveAsProxy(rasterProps);
    saveAs.saveAs("c:\\temp\\image1.img", null, "IMAGINE Image");
Setting a NoData value
To set a NoData value on a raster, use the following code:
static void setNoDataSaveAs(IRaster raster, IWorkspace ws, String sFormat, String
    sName)throws Exception{
    IRasterProps rasterProps = new IRasterPropsProxy(raster);

    //For a multiband image, for example, three bands; and the NoData value for each band are not same.
    int[] nodata = {
        255, 255, 236

    //Or if the raster has one band or NoData values for each band are the same, the following call can be used.

    ISaveAs saveas = new ISaveAsProxy(raster);
    saveas.saveAs(sName, ws, sFormat);

Development licensingDeployment licensing
ArcGIS for Desktop BasicArcGIS for Desktop Basic
ArcGIS for Desktop StandardArcGIS for Desktop Standard
ArcGIS for Desktop AdvancedArcGIS for Desktop Advanced
Engine Developer KitEngine