欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Arcgis Engine 实现单波段栅格图层色带渲染(包括色带反转)

程序员文章站 2022-06-11 16:14:25
...

    最近有同事问我ArcMap中的色带反转怎么实现,刚好我在整理以前的项目,找到了一些源码备忘。

            Arcgis Engine 实现单波段栅格图层色带渲染(包括色带反转)

    先说下思路,简单来说就是得到栅格图层的IRasterRenderer,转换为IRasterStretchColorRampRenderer,在将这个接口转换为IRasterStretch,IRasterStretch中有个Invert的方法可以控制色带是否反转。下面详细说下实现过程。

    1、重写一个可以添加图片的ComboBox

    public partial class ComboboxSymbol : ComboBox
    {
        public ComboboxSymbol()
        {
            DrawMode = DrawMode.OwnerDrawFixed;
            DropDownStyle = ComboBoxStyle.DropDownList;
        }
        //重写函数
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            try
            {
                //显示图片
                Image image = (Image)Items[e.Index];
                System.Drawing.Rectangle rect = e.Bounds;
                e.Graphics.DrawImage(image, rect);
            }
            catch
            {
            }
            finally
            {
                base.OnDrawItem(e);
            }
        }
    }

2、设计一个窗体,就下图中的几个按钮,另外添加一个隐藏显示的AxSymbologyControl,用来调用显示系统色带

Arcgis Engine 实现单波段栅格图层色带渲染(包括色带反转)

3、窗体下的代码如下

        private IRasterLayer _pRasyerLayer = null;
        
        public ColorRamp(IRasterLayer pRasterLayer,int X,int Y)
        {
            InitializeComponent();
            _pRasyerLayer = pRasterLayer;
            this.Location = new System.Drawing.Point(X, Y); //得到当前鼠标位置,用来确定窗体显示的位置
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LoadSymbolProperty(IRasterLayer mRasterLayer)
        {
            IRasterStretchColorRampRenderer pStretchRen;
            try
            {
                pStretchRen = (IRasterStretchColorRampRenderer)mRasterLayer.Renderer;
                IRasterStretch pRasterStretch = pStretchRen as IRasterStretch;
                if (pRasterStretch.Invert)
                {
                    ckInvert.Checked = true;
                }
            }
            catch (Exception ex)
            {
                WriteLog.LogWritter.WriteLogToFile(ex.Message);
                return;
            }
            try
            {
                //加载系统样式
                string sInstall = ESRI.ArcGIS.RuntimeManager.ActiveRuntime.Path;
                string defaultStyle = System.IO.Path.Combine(sInstall, "Styles\\ESRI.ServerStyle");

                axSymbologyControl1.LoadStyleFile(defaultStyle);
                axSymbologyControl1.StyleClass = esriSymbologyStyleClass.esriStyleClassColorRamps;
                ISymbologyStyleClass symbologyStyleClass = axSymbologyControl1.GetStyleClass(esriSymbologyStyleClass.esriStyleClassColorRamps);

                IStyleGalleryItem styleGalleryItem = new ServerStyleGalleryItem();
                styleGalleryItem.Item = pStretchRen.ColorRamp;
                styleGalleryItem.Name = "mySymbol";
                symbologyStyleClass.AddItem(styleGalleryItem, 0);
                symbologyStyleClass.SelectItem(0);

                //将样式转为图片并添加入ComboBox
                for (int i = 0; i < symbologyStyleClass.get_ItemCount(symbologyStyleClass.StyleCategory); i++)
                {
                    stdole.IPictureDisp picture = symbologyStyleClass.PreviewItem(symbologyStyleClass.GetItem(i), cbSymbol.Width, cbSymbol.Height);
                    System.Drawing.Image image = System.Drawing.Image.FromHbitmap(new System.IntPtr(picture.Handle));
                    cbSymbol.Items.Add(image);
                }
                if (cbSymbol.Items.Count > 0)
                    cbSymbol.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                WriteLog.LogWritter.WriteLogToFile(ex.Message);
            }
        }
        private void SetRasterSymbol(IRasterLayer mRasterLayer)
        {

            try
            {
                //获取选择的序号
                int symbol_index = cbSymbol.SelectedIndex;
                ISymbologyStyleClass symbologyStyleClass = axSymbologyControl1.GetStyleClass(esriSymbologyStyleClass.esriStyleClassColorRamps);
                IStyleGalleryItem mStyleGalleryItem = symbologyStyleClass.GetItem(symbol_index);
                //获取选择的符号
                IColorRamp colorramp_select = (IColorRamp)mStyleGalleryItem.Item;
                IRasterStretchColorRampRenderer pStretchRen = (IRasterStretchColorRampRenderer)mRasterLayer.Renderer;
                pStretchRen.ColorRamp = colorramp_select;
                IRasterStretch pRasterStretch = pStretchRen as IRasterStretch;
                //控制色带是否反转
                if (ckInvert.Checked)
                {
                    pRasterStretch.Invert = true;
                }
                else
                {
                    pRasterStretch.Invert = false;
                }
            }
            catch (Exception ex)
            {
                WriteLog.LogWritter.WriteLogToFile(ex.Message);
                this.Close();
            }

        }
        private void ColorRamp_Load(object sender, EventArgs e)
        {
            LoadSymbolProperty(_pRasyerLayer);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            SetRasterSymbol(_pRasyerLayer);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }

 

 

 

相关标签: GIS