|
IWorkspaceFactory workspaceFact = new RasterWorkspaceFactoryClass(); IRasterWorkspace2 rasterwr = (IRasterWorkspace2)workspaceFact.OpenFromFile("E:\\delete\\xuanzhi\\grid", 0); IRasterDataset rasterdataset = rasterwr.OpenRasterDataset("tingrid"); IRasterBandCollection rasterbands = (IRasterBandCollection)rasterdataset; IRasterBand rasterband = rasterbands.Item(0); IRawPixels rawpixels = (IRawPixels)rasterbands.Item(0); IRasterProps rasterpro = (IRasterProps)rasterband;
IRasterDataset2 rasterDataset2 = (IRasterDataset2)rasterdataset; IRaster raster = rasterDataset2.CreateFullRaster();
IRaster2 raster2 = (IRaster2)raster;
IPnt pBlockSize = new PntClass(); IEnvelope envelope = rasterpro.Extent; pBlockSize.SetCoords(envelope.Width,envelope.Height);
IPixelBlock pixelBlock = raster2.CreateCursorEx(pBlockSize).PixelBlock; int w = pixelBlock.Width; int h = pixelBlock.Height; //read the first pixel block IPnt topleftCorner = new PntClass(); topleftCorner.SetCoords(0, 0); raster.Read(topleftCorner, pixelBlock); //modify one pixel value at location (assume the raster has a pixel type of uchar) IPixelBlock3 pixelBlock3 = (IPixelBlock3)pixelBlock; System.Array pixels = (System.Array)pixelBlock3.get_PixelData(0);
for (int i = 0; i <w; i++) for (int j = 0; j <h; j++) if (i == j) pixels.SetValue(Convert.ToByte(100),i,j);
pixelBlock3.set_PixelData(0,(System.Object)pixels); //write the modified pixel block to the raster dataset IRasterEdit rasterEdit = (IRasterEdit)raster; rasterEdit.Write(topleftCorner, pixelBlock);
|