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

该博客主要介绍了栅格像素块的读取和像素值的修改。在栅格像素块读取方面,涉及Ipnt、IPixelBlock、IPixelBlock3等接口;在像素值修改方面,介绍了IRasterEdit接口及相关源码,还提及不同语言处理像素值传递的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

 

1 栅格像素块的读取

1.1 Ipnt接口

1.2 IPixelBlock接口

1.3 IPixelBlock3

1.4 栅格像素块的读取

2 栅格像素值的修改

2.1 IRasterEdit接口

2.2 像素值修改源码


1 栅格像素块的读取

1.1 Ipnt接口

1.2 IPixelBlock接口

 

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

 AllPropertiesMethodsDescription
Read-only propertyBytesPerPixelThe number of bytes per pixel for the PixelBlock.
MethodClearClears a given plane (sets to NoData).
MethodGetNoDataMaskValGets the NoData mask value for a specified pixel.
MethodGetValThe value for a specified pixel.
MethodHasNoDataChecks if this PixelBlock contains NoData.
Read-only propertyHeightThe height of the PixelBlock in pixels.
MethodMaskGenerates NoData Mask using a given NoData value.
Read/write propertyNoDataMaskThe NoData mask for a specified plane.
Read-only propertyNoDataMaskByRefThe NoData mask for a specified plane.
Read/write propertyPixelDataAn array of pixels for a specified plane.
Read-only propertyPixelDataByRefA pointer to an array of pixels for a specified plane.
Read/write propertyPixelTypeThe pixel type of the PixelBlock.
Read-only propertyPlanesThe number of pixel arrays contained in the PixelBlock.
Read-only propertyWidthThe 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.

Classes that implement IPixelBlock3

ClassesDescription
PixelBlockEsri 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接口

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变量为鼠标点击处的坐标,代码见这里

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薛桂琴

喜欢的读者,可以打赏鼓励一下

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值