C#+AE 使用pixelBlock3实现栅格数据逐项元操作:希望读取double类型的数据,并修改像元值,代码如下:
``` private void BuildRoute3(IRaster pRaster)
{
IRasterProps rasterProps = (IRasterProps)pRaster;
double[,] arrayvalue = new double[rasterProps.Height, rasterProps.Width];
IPnt pBlockSize = new PntClass();//栅格像素点
pBlockSize.SetCoords(1, 1);//设置像素块大小
IPixelBlock pixelBlock = pRaster.CreatePixelBlock(pBlockSize);//按指定大小创建像素块
IPnt topleft = new PntClass();
for (int i = 0; i < rasterProps.Height; i++)
{
for (int j = 0; j < rasterProps.Width; j++)
{
topleft.SetCoords(i, j);
pRaster.Read(topleft, pixelBlock);//读取指定位置像素块
object obj = pixelBlock.GetVal(0, 0, 0);
arrayvalue[i, j] = Convert.ToDouble(obj);
}
}
double[,] arrayResultvalue = new double[rasterProps.Height, rasterProps.Width];
//修改像元值,满足条件的赋为10
int x = 250;
int y = 277;
int sum = 0;
while (x > 0 && y > 0)
{
double value = arrayvalue[x, y];
double value1 = arrayvalue[x - 1, y - 1];
double value2 = arrayvalue[x, y - 1];
double value3 = arrayvalue[x - 1, y];
int no = 0; //最大值下标
if (value1 >= value2)
{
if (value1 >= value3)
{
no = 1;
}
else
{
no = 3;
}
}
else
{
if (value3 >= value2)
{
no = 3;
}
else
{
no = 2;
}
}
switch (no)
{
case 1:
arrayResultvalue[x - 1, y - 1] = 10;
x = x - 1;
y = y - 1;
break;
case 2:
arrayResultvalue[x, y - 1] = 10;
y = y - 1;
break;
case 3:
arrayResultvalue[x - 1, y] = 10;
x = x - 1;
break;
}
sum++;
}
if (y > 0)
{
while (y > 0)
{
arrayResultvalue[x , y] = 10;
y--;
sum++;
}
}
else
{
while (x > 0)
{
arrayResultvalue[x, y] = 10;
x--;
sum++;
}
}
//修改后的值转为byte重新写入栅格
byte[,] transResult = new byte[rasterProps.Height, rasterProps.Width];
for (int i = 0; i < rasterProps.Height; i++)
{
for (int j = 0; j < rasterProps.Width; j++)
{
if (arrayResultvalue[i, j] != 10)
{
arrayResultvalue[i, j] = 0;
}
transResult[i, j] = (byte)arrayResultvalue[i, j];
}
}
IPnt dealBlockSize = new PntClass();
dealBlockSize.SetCoords(rasterProps.Height, rasterProps.Width);
IPixelBlock3 pixelBlock3 = pRaster.CreatePixelBlock(dealBlockSize) as IPixelBlock3;
IRasterEdit rasterEdit = pRaster as IRasterEdit;
topleft.SetCoords(0, 0);//设置左上角点位置为写入起始定位
pixelBlock3.set_PixelData(0, (System.Object)transResult);//将值设置到像素块中
rasterEdit.Write(topleft, (IPixelBlock)pixelBlock3);//将像素块写入到栅格图像
}
报错如下(复制链接在浏览器可以查看图片):
![图片说明](https://img-ask.csdn.net/upload/201901/03/1546506224_922482.jpg)