IPixelBlock3.set_PixelData – Unbox before calling
When I first dug into .NET several years ago, I remember reading about boxing and unboxing but haven’t really had to deal with it. Until now. I had some code similar to that below that writes an array of shorts to an ArcGIS raster. I was getting an ArgumentException: Value does not fall within the expected range. So I added code that simply copies the array to a new identical array, and now it works. The only thing I can think is that the values in the array are boxed, while set_PixelData expects unboxed values. Wish I had time to figure out why it works.
if (inValues is float[,])
outArray = ConvertFloat2Int16((float[,])inValues, ((float)inProps.NoDataValue), outNodataval);
else if (inValues is Int16[,])
//outArray = (Int16[,])inValues; this causes set_PixelData to choke…
* I’m not sure why this next step is needed, but it is.
* Perhaps some issue with unboxing ?
outArray = new Int16[inProps.Width, inProps.Height];
for (int i = 0; i < outArray.GetLength(0); i++) for (int j = 0; j < outArray.GetLength(1); j++) outArray[i, j] = ((Int16[,])inValues)[i, j]; } else throw new Exception("unsupported raster value type: " + inValues.ToString()); [/sourcecode]