ArcObect开发的生成可视化的下拉框式颜色带

使用ArcObject开发的下拉框式的颜色带,用以生成可视化形式的ColorRamp,方便调用。

开发环境:ArcGIS10.1(ArcObject),VS2010

界面设计:如图1所示。

 

图1 功能界面设计

  

代码如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.Collections;
 10 using ESRI.ArcGIS.Display;
 11 using ESRI.ArcGIS.esriSystem;
 12 using ESRI.ArcGIS.Geometry;
 13 
 14 namespace GetIColorRamp
 15 {
 16     public partial class Form1 : Form
 17     {
 18         
 19         private ArrayList EnumStyleItem = new ArrayList();
 20         private IGradientFillSymbol m_FillSymbol;
 21         private IColorRamp m_ColorRamp;
 22 
 23         public Form1()
 24         {
 25             InitializeComponent();
 26 
 27             DrawColorRamp();
 28             colorComboBox.SelectedIndex = 0;
 29             pictureBox1.Image = colorComboBox.SelectedItem as Image;
 30 
 31 
 32             //若想获取选中的IColorRamp,可按选择的索引号在类型为ArrayList的数组EnumStyleItem中获取,获取方式可参考以下代码
 33             //IColorRamp pColorRamp = (IColorRamp)EnumStyleItem[colorComboBox.SelectedIndex];
 34         }
 35 
 36         private Image SymbolToBitmap(IGradientFillSymbol iSymbol, int iStyle, int iWidth, int iHeight)
 37         {
 38             IntPtr iHDC = new IntPtr();
 39             Bitmap iBitmap = new Bitmap(iWidth, iHeight);
 40             Graphics iGraphics = System.Drawing.Graphics.FromImage(iBitmap);
 41             tagRECT itagRECT;
 42             IEnvelope iEnvelope = new EnvelopeClass() as IEnvelope;
 43             IDisplayTransformation iDisplayTransformation;
 44             IPoint iPoint;
 45             IGeometryCollection iPolyline;
 46             IGeometryCollection iPolygon;
 47             IRing iRing;
 48             ISegmentCollection iSegmentCollection;
 49             IGeometry iGeometry = null;
 50             object Missing = Type.Missing;
 51             iEnvelope.PutCoords(0, 0, iWidth, iHeight);
 52             itagRECT.left = 0;
 53             itagRECT.right = iWidth;
 54             itagRECT.top = 0;
 55             itagRECT.bottom = iHeight;
 56             iDisplayTransformation = new DisplayTransformationClass();
 57             iDisplayTransformation.VisibleBounds = iEnvelope;
 58             iDisplayTransformation.Bounds = iEnvelope;
 59             iDisplayTransformation.set_DeviceFrame(ref itagRECT);//DeviceFrame
 60             iDisplayTransformation.Resolution = iGraphics.DpiX / 100000;
 61             iHDC = iGraphics.GetHdc();
 62             //获取Geometry
 63             if (iSymbol is ESRI.ArcGIS.Display.IMarkerSymbol)
 64             {
 65                 switch (iStyle)
 66                 {
 67                     case 0:
 68                         iPoint = new ESRI.ArcGIS.Geometry.Point();
 69                         iPoint.PutCoords(iWidth / 2, iHeight / 2);
 70                         iGeometry = iPoint;
 71                         break;
 72                     default:
 73                         break;
 74                 }
 75             }
 76             else if (iSymbol is ESRI.ArcGIS.Display.ILineSymbol)
 77             {
 78                 iSegmentCollection = new ESRI.ArcGIS.Geometry.Path() as ISegmentCollection;
 79                 iPolyline = new ESRI.ArcGIS.Geometry.Polyline() as IGeometryCollection;
 80                 switch (iStyle)
 81                 {
 82                     case 0:
 83                         iSegmentCollection.AddSegment(CreateLine(0, iHeight / 2, iWidth, iHeight / 2) as ISegment, ref Missing, ref Missing);
 84                         iPolyline.AddGeometry(iSegmentCollection as IGeometry, ref Missing, ref Missing);
 85                         iGeometry = iPolyline as IGeometry;
 86                         break;
 87                     case 1:
 88                         iSegmentCollection.AddSegment(CreateLine(0, iHeight / 4, iWidth / 4, 3 * iHeight / 4) as ISegment, ref Missing, ref Missing);
 89                         iSegmentCollection.AddSegment(CreateLine(iWidth / 4, 3 * iHeight / 4, 3 * iWidth / 4, iHeight / 4) as ISegment, ref Missing, ref Missing);
 90                         iSegmentCollection.AddSegment(CreateLine(3 * iWidth / 4, iHeight / 4, iWidth, 3 * iHeight / 4) as ISegment, ref Missing, ref Missing);
 91                         iPolyline.AddGeometry(iSegmentCollection as IGeometry, ref Missing, ref Missing);
 92                         iGeometry = iPolyline as IGeometry;
 93                         break;
 94                     default:
 95                         break;
 96                 }
 97             }
 98             else if (iSymbol is ESRI.ArcGIS.Display.IFillSymbol)
 99             {
100                 iSegmentCollection = new ESRI.ArcGIS.Geometry.Ring() as ISegmentCollection;
101                 iPolygon = new ESRI.ArcGIS.Geometry.Polygon() as IGeometryCollection;
102                 switch (iStyle)
103                 { 
104                     case 0:
105                         iSegmentCollection.AddSegment(CreateLine(1, iHeight - 1, iWidth - 2, iHeight - 1) as ISegment, ref Missing, ref Missing);
106                         iSegmentCollection.AddSegment(CreateLine(iWidth - 2, iHeight - 1, iWidth - 2, 2) as ISegment, ref Missing, ref Missing);
107                         iSegmentCollection.AddSegment(CreateLine(iWidth - 2, 2, 1, 2) as ISegment, ref Missing, ref Missing);
108                         iRing = iSegmentCollection as IRing;
109                         iRing.Close();
110                         iPolygon.AddGeometry(iSegmentCollection as IGeometry, ref Missing, ref Missing);
111                         iGeometry = iPolygon as IGeometry;
112                         break;
113                     default:
114                         break;
115                 }
116             }
117             else if (iSymbol is ESRI.ArcGIS.Display.ISimpleTextSymbol)
118             {
119                 switch (iStyle)
120                 { 
121                     case 0:
122                         iPoint = new ESRI.ArcGIS.Geometry.Point();
123                         iPoint.PutCoords(iWidth / 2, iHeight / 2);
124                         iGeometry = iPoint;
125                         break;
126                     default: 
127                         break;
128                 }
129             }
130             if (iGeometry == null)
131             {
132                 MessageBox.Show("几何对象不符合要求!", "错误");
133                 return null;
134             }
135             ISymbol pOutputSymbol = iSymbol as ISymbol;
136             pOutputSymbol.SetupDC(iHDC.ToInt32(), iDisplayTransformation);
137             pOutputSymbol.Draw(iGeometry);
138             pOutputSymbol.ResetDC();
139             iGraphics.ReleaseHdc(iHDC);
140             iGraphics.Dispose();
141             return iBitmap;
142         }
143 
144         private ILine CreateLine(int x1, int y1, int x2, int y2)
145         {
146             IPoint pnt1 = new PointClass();
147             pnt1.PutCoords(x1, y1);
148             IPoint pnt2 = new PointClass();
149             pnt2.PutCoords(x2, y2);
150             ILine ln = new LineClass();
151             ln.PutCoords(pnt1, pnt2);
152             return ln;
153         }
154 
155         private void DrawColorRamp()
156         {
157             string strDefaultStyleFileName = string.Format("{0}\\Styles\\ESRI.ServerStyle", Application.StartupPath);
158             IStyleGallery styleGallery = new ServerStyleGalleryClass();
159             IStyleGalleryItem styleGalleryItem = new ServerStyleGalleryItemClass();
160             IStyleGalleryStorage styleGalleryStorage = styleGallery as IStyleGalleryStorage;
161             styleGalleryStorage.AddFile(strDefaultStyleFileName);
162             IEnumStyleGalleryItem enumStyleGalleryItem = styleGallery.get_Items("Color Ramps", strDefaultStyleFileName, "");
163             enumStyleGalleryItem.Reset();
164             styleGalleryItem = enumStyleGalleryItem.Next();
165             while (styleGalleryItem != null)
166             {
167                 m_ColorRamp = (IColorRamp)styleGalleryItem.Item;
168                 EnumStyleItem.Add(m_ColorRamp);
169                 //新建m_FillSymbol和m_colorRamp
170                 m_FillSymbol = new GradientFillSymbol();
171                 m_FillSymbol.GradientAngle = 0;
            m_FillSymbol.IntervalCount = 255; //非常关键,涉及到调用ColorRamp中每种颜色的数量,否则每个颜色带只有5中颜色。
               
m_FillSymbol.GradientPercentage = 1; //同上
 
172 m_FillSymbol.ColorRamp = m_ColorRamp; 173 pictureBox1.Image = SymbolToBitmap(m_FillSymbol, 0, pictureBox1.Width, pictureBox1.Height); 174 imageList1.Images.Add(m_ColorRamp.Name, pictureBox1.Image); 175 colorComboBox.Items.Add(pictureBox1.Image); 176 styleGalleryItem = enumStyleGalleryItem.Next(); 177 } 178 } 179 180 private void colorComboBox_DrawItem(object sender, DrawItemEventArgs e) 181 { 182 e.DrawBackground();//绘制背景 183 e.DrawFocusRectangle();//绘制焦点框 184 //绘制图例 185 Rectangle iRectangle = new Rectangle(e.Bounds.Left, e.Bounds.Top, 215, 27); 186 Bitmap getBitmap = new Bitmap(imageList1.Images[e.Index]); 187 e.Graphics.DrawImage(getBitmap, iRectangle); 188 } 189 190 private void colorComboBox_SelectionChangeCommitted(object sender, EventArgs e) 191 { 192 pictureBox1.Image = colorComboBox.SelectedItem as Image; 193 } 194 } 195 }


以上为该程序的源码,完整程序下载地址:IColorRamp.rar

posted @ 2014-02-13 14:13  zhzhx0318  阅读(761)  评论(0编辑  收藏  举报