刚刚说的是获取像素值。如果要进一步改变像素值的话,需要用到IRasterEdit接口的Write方法。Write方法的参数有两个,一个是在栅格图像中的左上角位置(行列值不是坐标值),另一个就是获取像素值时提到的PixelBlock.
执行完Write方法后要对实现IRasterEdit接口的对象进行释放。
1 public void ChangePixelValue(double xMax, double xMin, double yMax, double yMin,double[,] PixelChanged)
2 {
3 IRaster pRaster = thisRasterLayer.Raster;
4 IRaster2 pRaster2 = pRaster as IRaster2;
5
6 //地图坐标转换为图中行列值
7 rowMax = pRaster2.ToPixelRow(yMin);
8 rowMin = pRaster2.ToPixelRow(yMax);
9 columnMin = pRaster2.ToPixelColumn(xMin);
10 columnMax = pRaster2.ToPixelColumn(xMax);
11
12 int Height = rowMax - rowMin + 1;
13 int Width = columnMax - columnMin + 1;
14
15 //按照需要的大小建立一个空的PixelBlock3
16 IPnt blocksize = new PntClass();
17 blocksize.SetCoords(Width, Height);
18
19 IPixelBlock3 pPixelBlock3 = pRaster.CreatePixelBlock(blocksize) as IPixelBlock3;
20
21 System.Array pixels = (System.Array)pPixelBlock3.get_PixelData(0);
22
23 //为新建的PixelBlock赋值
24 try
25 {
26 for (int i = 0; i < Height; i++)
27 {
28 for (int j = 0; j < Width; j++)
29 {
30 pixels.SetValue(Convert.ToByte(PixelChanged[i,j]), j, i);
31 }
32 }
33 }
34 catch (Exception ex)
35 {
36 MessageBox.Show(ex.Message);
37 }
38
39 //把像素值赋予新建的PixelBlock3
40 pPixelBlock3.set_PixelData(0, pixels);
41
42 //PixelBlock3应在的位置
43 blocksize.SetCoords(columnMin, rowMin);
44
45 //改变的像素值写入图层
46 IRasterEdit pRasterEdit = pRaster as IRasterEdit;
47 pRasterEdit.Write(blocksize, (IPixelBlock)pPixelBlock3);
48 pRasterEdit.Refresh();
49
50 System.Runtime.InteropServices.Marshal.ReleaseComObject(pRasterEdit);
51 }
修改过的图层需要保存,不然重新加载时像素值还是没有修改过。
1 //要保存的图层;
2 IRasterLayer pRasterLayer = axMapControl1.get_Layer(0) as IRasterLayer;
3 IRaster pRaster = pRasterLayer.Raster;
4 IRaster2 pRaster2 = pRaster as IRaster2;
5
6 ISaveAs pSaveAs = pRaster2 as ISaveAs;
7 pSaveAs.SaveAs(@"F:\Work\DEM高程矩阵\DEM高程矩阵\bin\Debug\渲染图层2.tif", null, "TIFF");
文章地址:http://hi.baidu.com/fzoxeoomwvabkqe/item/6f92b02b30d59859469962e7
AE IRasterCursor 改变栅格图层像素值,布布扣,bubuko.com
时间: 2024-12-29 10:24:42