基于C#的ArcEngine二次开发教程(16):读取栅格影像的像素块并修改其灰度值

目录

 

1 栅格像素块的读取

1.1 Ipnt接口

1.2 IPixelBlock接口

1.3 IPixelBlock3

1.4 栅格像素块的读取

2 栅格像素值的修改

2.1 IRasterEdit接口

2.2 像素值修改源码


1 栅格像素块的读取

1.1 Ipnt接口

基于C#的ArcEngine二次开发教程(16):读取栅格影像的像素块并修改其灰度值_第1张图片

1.2 IPixelBlock接口

基于C#的ArcEngine二次开发教程(16):读取栅格影像的像素块并修改其灰度值_第2张图片

 

Description

The IPixelBlock3 interface provides all the functionality of IPixelBlock interface, plus more properties on mask based NoData and SafeArray handling.

Basically, there are two ways to work with NoData mask and pixel values. They are by value (PixelData and NoDataMask properties) and by reference (PixelDataByRef and NoDataMaskByRef). Normally passing pixel values by reference is recommented since it saves memory. However for Java and .Net, passing by value (PixelData) should be used.

Members

  AllPropertiesMethods Description
BytesPerPixel The number of bytes per pixel for the PixelBlock.
Clear Clears a given plane (sets to NoData).
GetNoDataMaskVal Gets the NoData mask value for a specified pixel.
GetVal The value for a specified pixel.
HasNoData Checks if this PixelBlock contains NoData.
Height The height of the PixelBlock in pixels.
Mask Generates NoData Mask using a given NoData value.
NoDataMask The NoData mask for a specified plane.
NoDataMaskByRef The NoData mask for a specified plane.
PixelData An array of pixels for a specified plane.
PixelDataByRef A pointer to an array of pixels for a specified plane.
PixelType The pixel type of the PixelBlock.
Planes The number of pixel arrays contained in the PixelBlock.
Width The width of the PixelBlock in pixels.


1.3 IPixelBlock3

The IPixelBlock3 interface provides all the functionality of IPixelBlock interface, plus more properties on mask based NoData and SafeArray handling.

Basically, there are two ways to work with NoData mask and pixel values. They are by value (PixelData and NoDataMask properties) and by reference (PixelDataByRef and NoDataMaskByRef). Normally passing pixel values by reference is recommented since it saves memory. However for Java and .Net, passing by value (PixelData) should be used.

基于C#的ArcEngine二次开发教程(16):读取栅格影像的像素块并修改其灰度值_第3张图片

Classes that implement IPixelBlock3

Classes Description
PixelBlock Esri PixelBlock, a container of pixel data.

1.4 栅格像素块的读取

 private void 读像素块ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //定义像素块的尺寸
            IPnt size = new PntClass();
            size.X = 3;
            size.Y = 3;
            //获取栅格图层
            IRasterLayer rstlyer = axMapControl1.get_Layer(0) as IRasterLayer;
            IRaster rst = rstlyer.Raster;
            //创建像素块
            IPixelBlock pb = rst.CreatePixelBlock(size);
            //定义像素块起始位置坐标
            IPnt tpl = new PntClass();
            tpl.X = col - 1;
            tpl.Y = row - 1;

            //读取数据到像素块中
            rst.Read(tpl, pb);

            //数据存储到数组中
            IPixelBlock3 pb3 = pb as IPixelBlock3;
            System.Array pixs = pb3.get_PixelData(0) as System.Array;
            //遍历获取像素值
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    string value = Convert.ToString(pixs.GetValue(i, j));
                    MessageBox.Show(value);
                }
            }
        }

2 栅格像素值的修改

2.1 IRasterEdit接口

基于C#的ArcEngine二次开发教程(16):读取栅格影像的像素块并修改其灰度值_第4张图片

2.2 像素值修改源码

 

        private void 修改像素ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //获取栅格图层
            IRasterLayer rstlyer = axMapControl1.get_Layer(0) as IRasterLayer;
            IRaster rst = rstlyer.Raster;
            //定义像素块尺寸
            IPnt size = new PntClass();
            size.X = 1;
            size.Y = 1;
            //创建像素块
            IPixelBlock pb = rst.CreatePixelBlock(size);
            //定义像素块起始位置
            IPnt tpl = new PntClass();
            tpl.X = col;
            tpl.Y = row;
            //修改像素块的值
            IRasterEdit rstedit = rst as IRasterEdit;
            //将像素块中的值保存到数组中
            IPixelBlock3 pb3 = pb as IPixelBlock3;
            System.Array ay = pb3.get_PixelData(0) as System.Array;
            //为二维数组赋值
            ay.SetValue(Convert.ToInt16(85), 0, 0);
            //为像素块赋值
            pb3.set_PixelData(0, ay);
            //将像素变更结果写入栅格影像中
            rstedit.Write(tpl, pb3 as IPixelBlock);
            MessageBox.Show("修改完成!");
            axMapControl1.Refresh();
        }

注:代码中的col和row变量为鼠标点击处的坐标,代码见这里

 

你可能感兴趣的:(基于C#的ArcEngine二次开发教程(16):读取栅格影像的像素块并修改其灰度值)