Can't write a one-cell pixel block to the last column or last row of raster

Discussion created by ericmweber on Jul 23, 2012
I have the following method for writing pixel edits to a raster:

        private void RectangularRasterEdit(IRaster raster, IPnt topLeft, int widthInCells, int heightInCells, List<int> newPixelValues)
            //IRasterBand rasterBand = ((IRasterBandCollection)raster).Item(0);

            //pixel block size
            IPnt blockSize = new PntClass();
            blockSize.X = widthInCells;
            blockSize.Y = heightInCells;

            //create pixel block
            //IPixelBlock pixelBlock = ((IRawPixels)rasterBand).CreatePixelBlock(blockSize);
            IPixelBlock pixelBlock = raster.CreatePixelBlock(blockSize);

            //build array of new values to be written to raster
            short[,] pixels = new short[widthInCells, heightInCells];
            int newValueIndex = 0;
            for (int i = 0; i < widthInCells; i++)
                for (int j = 0; j < heightInCells; j++)
                    pixels[i, j] = (short)newPixelValues[newValueIndex];

            //update pixel block with new pixel array
            ((IPixelBlock3)pixelBlock).set_PixelData(0, pixels);
            //write changed pixelblock to raster
            //((IRawPixels)rasterBand).Write(topLeft, pixelBlock);
            ((IRasterEdit)raster).Write(topLeft, pixelBlock);

This method can handle rectangles of any size, but I use it mostly for one-cell edits (pixelBlock is 1 X 1). The problem is that when I try to edit a cell in the rightmost column or in the bottom row of the raster, the IRasterEdit.Write method fails (with a generic HRESULT). I have tried the IRawPixels.Write method as well (the code for that is in comments above), but get the same error.

For example, say I have a raster that is 6 pixels by 6 pixels. If I pass a 1X1 pixelBlock to the write method, and the top left corner contains a 5 for either the row or column, it fails.

Also, after some testing, I verified that there is no problem reading a 1X1 pixel block using the same top left corner. The problem only occurs when trying to write.

Any ideas what might be causing this issue?