重写Combobox实现颜色带(ColorRamp)Symbol预览与选择——总结

news/2024/11/25 1:06:13/

 效果图

效果图

在地图符号化的时候经常要预览与选择Color Ramp Symbol,像ArcMap那样将ColorRamp符号放在Combobox现实既方便也实用,下面是实现原理与方法。

    1、首先解决的是如何将图片放在Combobox中,要知道vs2005乃至vs2008的Combobox都不可以加载图片的,所以需要重写一个Combobox控件;

    2、我们实现了将图片加载到Combobox中,下一步是如何将ColorRamp Symbol 这样的符号文件转换成图片,在查找ArcEngine实例代码中给我以启发,看下面一段代码:

   //Get and set the style class

    ISymbologyStyleClass symbologyStyleClass = axSymbologyControl1.GetStyleClass(axSymbologyControl1.StyleClass);

 

   //Preview an image of the symbol

 

   stdole.IPictureDisp picture = symbologyStyleClass.PreviewItem(m_styleGalleryItem, pictureBox1.Width, pictureBox1.Height);

   System.Drawing.Image image = System.Drawing.Image.FromHbitmap(new System.IntPtr(picture.Handle));

   pictureBox1.Image = image;

 

这样实现起来相对简单,我在网上看到好多自己写的直接是将ISymbol转换成Bmp的图片,代码如下:

      

      private System.Drawing.Bitmap SymbolToBitmp(ESRI.ArcGIS.Display.ISymbol pSymbol, int iwidth, int iheight, int lGap)

        {

            ///根据高宽创建图象

            Bitmap bmp = new Bitmap(iwidth, iheight);

            Graphics gImage = Graphics.FromImage(bmp);//转化成Graphics

            gImage.Clear(Color.White);                //清除底色,设置为白色

            double dpi = gImage.DpiX;                 //

 

            IEnvelope pEnvelope = new EnvelopeClass();//创建矩形范围

            pEnvelope.PutCoords(0, 0, (double)bmp.Width, (double)bmp.Height);

            //设置边界值

            tagRECT deviceRect;

            deviceRect.left = lGap;

            deviceRect.right = bmp.Width - lGap;

            deviceRect.top = lGap;

            deviceRect.bottom = bmp.Height - lGap;

 

            IDisplayTransformation pDisplayTransformation = new DisplayTransformationClass();

            pDisplayTransformation.VisibleBounds = pEnvelope;

            pDisplayTransformation.Bounds = pEnvelope;

            pDisplayTransformation.set_DeviceFrame(ref deviceRect);

            pDisplayTransformation.Resolution = dpi;

 

            //IGeometry pGeo = CreateSymShape(pSymbol, pEnvelope);

            IGeometry pGeo = CreateGeometryFromSymbol(pSymbol, pEnvelope);

            System.IntPtr hdc = new IntPtr();

            hdc = gImage.GetHdc();

 

            pSymbol.SetupDC((int)hdc, pDisplayTransformation);

            pSymbol.Draw(pGeo);

            pSymbol.ResetDC();

            gImage.ReleaseHdc(hdc);

            gImage.Dispose();

 

            return bmp;

 

        }

 

        private ESRI.ArcGIS.Geometry.IGeometry CreateGeometryFromSymbol(ISymbol sym, IEnvelope env)

        {

            if (sym is IMarkerSymbol)

            {

                IArea area = env as IArea;

                return area.Centroid as IGeometry;

            }

 

            else if (sym is ILineSymbol || sym is ITextSymbol)

            {

                IPolyline line = new PolylineClass();

                IPoint pt = new PointClass();

                pt.PutCoords(env.LowerLeft.X, (env.LowerLeft.Y + env.UpperRight.Y) / 2);

                line.FromPoint = pt;

                pt = new PointClass();

                pt.PutCoords(env.UpperRight.X, (env.LowerLeft.Y + env.UpperRight.Y) / 2);

                line.ToPoint = pt;

                return line as IGeometry;

            }

            else if (sym is IFillSymbol)

            {

                IPolygon polygon = new PolygonClass();

                IPointCollection ptCol = (IPointCollection)polygon;

                IPoint pt = new PointClass();

                pt.PutCoords(env.LowerLeft.X, env.LowerLeft.Y);

                ptCol.AddPoints(1, ref  pt);

                pt.PutCoords(env.UpperLeft.X, env.UpperLeft.Y);

                ptCol.AddPoints(1, ref pt);

                pt.PutCoords(env.UpperRight.X, env.UpperRight.Y);

                ptCol.AddPoints(1, ref pt);

                pt.PutCoords(env.LowerRight.X, env.LowerRight.Y);

                ptCol.AddPoints(1, ref pt);

                pt.PutCoords(env.LowerLeft.X, env.LowerLeft.Y);

                ptCol.AddPoints(1, ref pt);

                return polygon as IGeometry;

            }

            else

            {

                System.Windows.Forms.MessageBox.Show("找到一种特殊的符号!");

                return null;

            }

        }

不能说这种方法不好,但是我是没试验成功,个人感觉太繁琐,它需要将每一个符号转化成图片,速度堪忧啊。

 

3、我们已经实现了imag加载到Combobox 又可以将Isymbol to Image,下面是如何获取 ColorRamp的符号了。获取符号无非是从符号库中读取符号,这里我使用了Engine提供的一个符号控件axSymbologyControl(这么好东东为啥不用),代码如下:

 axSymbologyControl1.LoadStyleFile(Application.StartupPath + "//制图符号//底图符号.ServerStyle");

axSymbologyControl1.StyleClass = esriSymbologyStyleClass.esriStyleClassColorRamps;

这样所有符号库中的ColorRamp都被加载到axSymbologyControl1中了;我们可以从中获取我们想要的符号,好了说那么多了看一下全部代码和效果吧:

 

 private void Form_SetRasterColor_Load(object sender, EventArgs e)

        {

            LoadSymbolProperty(mRLayer);

        }

private void LoadSymbolProperty(IRasterLayer mRasterLayer)

        {

            IRasterStretchColorRampRenderer pStretchRen = (IRasterStretchColorRampRenderer)mRasterLayer.Renderer;

            try

            {

                axSymbologyControl1.LoadStyleFile(Application.StartupPath + "//制图符号//底图符号.ServerStyle");

                axSymbologyControl1.StyleClass = esriSymbologyStyleClass.esriStyleClassColorRamps;

 

                ISymbologyStyleClass symbologyStyleClass = axSymbologyControl1.GetStyleClass(esriSymbologyStyleClass.esriStyleClassColorRamps);

 

                //Create a new server style gallery item with its style set

                IStyleGalleryItem styleGalleryItem = new ServerStyleGalleryItem();

                styleGalleryItem.Item = pStretchRen.ColorRamp;

                styleGalleryItem.Name = "mySymbol";

                //Add the item to the style class and select it

                symbologyStyleClass.AddItem(styleGalleryItem, 0);

                symbologyStyleClass.SelectItem(0);

                for (int i = 0; i < symbologyStyleClass.get_ItemCount(symbologyStyleClass.StyleCategory); i++)

                {

                    stdole.IPictureDisp picture = symbologyStyleClass.PreviewItem(symbologyStyleClass.GetItem(i), cmb_symbol.Width, cmb_symbol.Height);

                    System.Drawing.Image image = System.Drawing.Image.FromHbitmap(newSystem.IntPtr(picture.Handle));

                    cmb_symbol.Items.Add(image);

                }

                if (cmb_symbol.Items.Count>0)

                cmb_symbol.SelectedIndex = 0;

                   // pictureBox1.Image = SymbolToBitmp(axSymbologyControl1, pictureBox1.Width, pictureBox1.Height, 0);

            }

            catch (Exception ex)

            {

                System.Diagnostics.Trace.WriteLine(ex.Message);

            }

       

        }

------------------------------以上是符号加载-------------------------------------

       private void btn_ssubmit_Click(object sender, EventArgs e)

        {

            SetRasterSymbol(mRLayer);

            this.DialogResult = DialogResult.OK;

            this.Close();

        }

        private void SetRasterSymbol(IRasterLayer mRasterLayer)

        {

            try

            {

                int symbol_index = cmb_symbol.SelectedIndex;//获取选择的序号

                ISymbologyStyleClass symbologyStyleClass = axSymbologyControl1.GetStyleClass(esriSymbologyStyleClass.esriStyleClassColorRamps);

                IStyleGalleryItem mStyleGalleryItem = symbologyStyleClass.GetItem(symbol_index);

                IColorRamp colorramp_select = (IColorRamp)mStyleGalleryItem.Item;//获取选择的符号

 

                //IColorRampSymbol mColorRampSymbol = new ColorRampSymbolClass();

                //mColorRampSymbol = (IColorRampSymbol)symbol_select;

 

                IRasterStretchColorRampRenderer pStretchRen = (IRasterStretchColorRampRenderer)mRasterLayer.Renderer;

                pStretchRen.ColorRamp = colorramp_select;

            }

            catch (Exception ex)

            {

                System.Diagnostics.Trace.WriteLine(ex.Message);

            }

        }------------------------------以上是选择符号并实例化-----------------------------

 

Combobox 重载:

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;

using System.Drawing;

namespace EngineClass_ToolBoxs

{

    // <summary>

    /// 重写了Combobox类,使其在下拉框中显示Symbol颜色带的图片

    /// </summary>

    /// <remarks>

     /// </remarks>

    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);

            }

        }

    }

 

}


http://www.ppmy.cn/news/323177.html

相关文章

音量和分贝(二)

放大电路有四种模型&#xff1a;电压放大电路&#xff08;对应电压增益Av&#xff09;&#xff1b;电流放大电路&#xff08;对应电流增益Ai&#xff09;&#xff1b;互阻放大电路&#xff08;对应互阻增益Ar&#xff09;&#xff1b;互导放大电路&#xff08;对应互导增益Ag&a…

leetcode - Best Time to Buy and Sell Stock III

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. 分析&#xff1a;和题1的只能购买一次&#xff0c;和题2购买不限次数&#xff0c;题…

【算法导论】学习笔记——第14章 数据结构的扩张

这一章节特别有意思。习题也比较多&#xff0c;但是很容易掌握。主要描述的就是在已知数据结构的基础上&#xff0c;通过增加或修改部分基础操作。来构造更加有效的新数据结构。14.1 动态数据统计本节主要介绍如何修改红黑树&#xff0c;使得可以在O(lgn)时间内确定顺序统计量&…

基于SYD8801的蓝牙遥控器设计方案【按键功能】【安卓手机功能演示】【ios功能演示】

SYD8801是一款低功耗高性能蓝牙低功耗SOC&#xff0c;集成了高性能2.4GHz射频收发机、32位ARM Cortex-M0处理器、128kB Flash存储器、以及丰富的数字接口。SYD8801片上集成了Balun无需阻抗匹配网络、高效率DCDC降压转换器&#xff0c;适合用于可穿戴、物联网设备等。具体可咨询…

ArcGIS Engine 数据渲染

分级渲染 /// <summary>/// 分级渲染/// </summary>/// <param name"pMapControl">数据视图</param>/// <param name"pFtLayer">图层</param>/// <param name"ClassCount">分为几类</param>///…

[CVPR2017] Weakly Supervised Cascaded Convolutional Networks论文笔记

https://www.csee.umbc.edu/~hpirsiav/papers/cascade_cvpr17.pdf Weakly Supervised Cascaded Convolutional Networks, Ali Diba, Vivek Sharma, Ali Pazandeh, Hamed Pirsiavash and Luc Van Gool 亮点 通过多任务叠加&#xff08;分类&#xff0c;分割&#xff09;提高了多…

蛇行矩阵-C语言

题目 蛇形矩阵是由1开始的自然数依次排列成的一个矩阵上三角形。 输入格式&#xff1a; 本题有多组数据&#xff0c;每组数据由一个正整数N组成。&#xff08;N不大于100&#xff09; 输出格式&#xff1a; 对于每一组数据&#xff0c;输出一个N行的蛇形矩阵。 两组输出之…

模拟数字电路->绪论

模拟数字电路 绪论信号相关放大电路模型抽象放大器 放大电路模型分析电压放大电路电流放大模型 主要性能指标增益Av,输入电阻Ri,输出电阻Ro频率响应和非线性失真非线性失真 问题总结题型输出电压,输出电阻放大倍数 课程无关 绪论 信号相关 信号:信息的载体 电信号源的电路表达…