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.

Int16[,] outArray;
if (inValues is float[,])
outArray = ConvertFloat2Int16((float[,])inValues, ((float[])inProps.NoDataValue)[0], 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]

Advertisements

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: