Error of writing raster

716
1
01-15-2014 05:58 PM
HaoliangYu
New Contributor
Hi, I am writing a raster editor toolbar for interactive raster edition. It is almost done but I meet an error when I try to save the modification to file finally. My method is to copy the loaded raster and write the modification to the new file. Here is my code:
// Code to get the new raster
IRasterWorkspace newRasterWorkspace = (IRasterWorkspace)newWorkspace;
IRasterDataset newRasterDataset = newRasterWorkspace.OpenRasterDataset(System.IO.Path.GetFileNameWithoutExtension(saveFileDialog.FileName));
IRasterBandCollection newRasterBandCollection = (IRasterBandCollection)newRasterDataset;
IRasterBand newRasterBand = newRasterBandCollection.Item(0);
IRasterDataset2 newRasterDataset2 = (IRasterDataset2)newRasterBand.RasterDataset;
IRaster newRaster = newRasterDataset2.CreateFullRaster();
IRasterProps newRasterProp = (IRasterProps)newRaster;

// I omit some code to get the extent of modified region

IPnt pos = new PntClass();
pos.SetCoords(maxCol - minCol + 1, maxRow - minRow + 1);
IPixelBlock pixelBlock = newRaster.CreatePixelBlock(pos);
pos.SetCoords(minCol, minRow);
newRaster.Read(pos, pixelBlock);

// Set new values
IPixelBlock3 pixelBlock3 = (IPixelBlock3)pixelBlock;
Array pixels = (Array)pixelBlock3.get_PixelData(0);


for (int i = 0; i < Editor.EditRecord.Count; i++)
{
    pixels.SetValue(Convert.ToByte(Editor.EditRecord.NewValue),
                         Editor.EditRecord.Position.Column - minCol,
                         Editor.EditRecord.Position.Row - minRow);
}

pixelBlock3.set_PixelData(0, (System.Object)pixels);
IRasterEdit newRasterEdit = (IRasterEdit)newRaster;

// Here comes the error:
newRasterEdit.Write(pos, (IPixelBlock)pixelBlock3);


I get the error information: Exception from HRESULT: 0x80041029. I do not find any information about the error code at ESRI help. The code looks very correct and I have no idea of the error.

btw, is there a way to directly save modification to the raster loaded at ArcMap?

Haoliang
0 Kudos
1 Reply
HaoliangYu
New Contributor
I have solved the problem by adding the following code in advance:
ESRI.ArcGIS.RuntimeManager.BindLicense(ESRI.ArcGIS.ProductCode.Desktop);
0 Kudos