|
|
|
|

基于ArcGIS Engine编写的ColorRamp下拉框

编写一个colorramp下拉框,无非就做两件事,首先将colorramp绘制成一个bitmap图片,其次就是将该图片绘制到combobox的item上。那么现看看后面一个问题怎么绘制到combobox上:vb.net代码实例首先在form上加载一个imagelist,将要绘制的图片存放到里面...

作者:snoopyxp来源:本站原创|2009年09月23日
编写一个colorramp下拉框,无非就做两件事,首先将colorramp绘制成一个bitmap图片,其次就是将该图片绘制到combobox的item上。
那么现看看后面一个问题怎么绘制到combobox上:vb.net代码实例

首先在form上加载一个imagelist,将要绘制的图片存放到里面(将以自己截个colorramp的图片),再加载combobox,将其Drawmode设置为owenrdrawfixed,然后在其DrawItem事件中编写

  1.   Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
  2.         Dim iRectangle As System.Drawing.Rectangle
  3.         If e.Index = -1 Or sender Is Nothing Then
  4.             Exit Sub
  5.         Else
  6.             '绘制背景
  7.             e.DrawBackground()
  8.             '绘制焦点框
  9.             e.DrawFocusRectangle()
  10.             '绘制图例
  11.             iRectangle = New System.Drawing.Rectangle(e.Bounds.Left + 1, e.Bounds.Top + 1, 117, 14)
  12.             e.Graphics.DrawImage(Me.ImageList1.Images(e.Index), iRectangle)
  13.         End If
  14.     End Sub
复制代码
上面一段代码就可以将imagelist中相应序号的图片绘制到相应序号的item上了,视觉部分完毕,很easy吧?


有了上面的过程我们就应该思考怎么样得到指定colorramp的图像,如果能得到colorramp的图像,只要替换imagelist中的图片就可以实现我们的目的。
注意:下面的代码首先新建一个ESRI.ARCGIS.Display.IGradientFillSymbol和一个ColorRamp

  1. Private Sub DrawColorRamp()
  2.         '新建 m_FillSymbol和m_ColorRamp

  3.         m_FillSymbol.ColorRamp = m_ColorRamp
  4.         Me.PictureBox1.Image = SymbolToBitmap(m_FillSymbol, 0, Me.PictureBox1.Width, Me.PictureBox1.Height)
  5.     End Sub
复制代码

  1. Friend Function SymbolToBitmap(ByVal iSymbol As ESRI.ARCGIS.Display.ISymbol, ByVal iStyle As Integer, ByVal iWidth As Integer, ByVal iHeight As Integer) As System.Drawing.Bitmap
  2.         Dim iHDC As New IntPtr
  3.         Dim iBitmap As System.Drawing.Bitmap
  4.         Dim iGraphics As System.Drawing.Graphics
  5.         Dim itagRECT As ESRI.ARCGIS.Display.tagRECT
  6.         Dim iEnvelope As ESRI.ARCGIS.Geometry.IEnvelope
  7.         Dim iDisplayTransformation As ESRI.ARCGIS.Display.IDisplayTransformation
  8.         Dim iPoint As ESRI.ArcGIS.Geometry.IPoint
  9.         Dim iPolyline As ESRI.ArcGIS.Geometry.IGeometryCollection
  10.         Dim iPolygon As ESRI.ArcGIS.Geometry.IGeometryCollection
  11.         Dim iRing As ESRI.ArcGIS.Geometry.IRing
  12.         Dim iSegmentCollection As ESRI.ArcGIS.Geometry.ISegmentCollection
  13.         Dim iGeometry As ESRI.ArcGIS.Geometry.IGeometry
  14.         iBitmap = New System.Drawing.Bitmap(iWidth, iHeight)
  15.         iGraphics = System.Drawing.Graphics.FromImage(iBitmap)
  16.         iEnvelope = New ESRI.ArcGIS.Geometry.Envelope
  17.         iEnvelope.PutCoords(0, 0, iWidth, iHeight)
  18.         With itagRECT
  19.             .left = 0
  20.             .right = iWidth
  21.             .top = 0
  22.             .bottom = iHeight
  23.         End With
  24.         iDisplayTransformation = New ESRI.ArcGIS.Display.DisplayTransformation
  25.         With iDisplayTransformation
  26.             .VisibleBounds = iEnvelope
  27.             .Bounds = iEnvelope
  28.             .DeviceFrame = itagRECT
  29.             .Resolution = iGraphics.DpiX
  30.         End With
  31.         iHDC = iGraphics.GetHdc.ToInt32
  32.         '获取Geometry
  33.         If TypeOf (iSymbol) Is ESRI.ArcGIS.Display.IMarkerSymbol Then
  34.             Select Case iStyle
  35.                 Case 0
  36.                     iPoint = New ESRI.ArcGIS.Geometry.Point
  37.                     iPoint.PutCoords(iWidth / 2, iHeight / 2)
  38.                     iGeometry = iPoint
  39.             End Select
  40.         ElseIf TypeOf (iSymbol) Is ESRI.ArcGIS.Display.ILineSymbol Then
  41.             iSegmentCollection = New ESRI.ArcGIS.Geometry.Path
  42.             iPolyline = New ESRI.ArcGIS.Geometry.Polyline
  43.             Select Case iStyle
  44.                 Case 0
  45.                     iSegmentCollection.AddSegment(CreateLine(0, iHeight / 2, iWidth, iHeight / 2))
  46.                     iPolyline.AddGeometry(iSegmentCollection)
  47.                     iGeometry = iPolyline
  48.                 Case 1
  49.                     iSegmentCollection.AddSegment(CreateLine(0, iHeight / 4, iWidth / 4, 3 * iHeight / 4))
  50.                     iSegmentCollection.AddSegment(CreateLine(iWidth / 4, 3 * iHeight / 4, 3 * iWidth / 4, iHeight / 4))
  51.                     iSegmentCollection.AddSegment(CreateLine(3 * iWidth / 4, iHeight / 4, iWidth, 3 * iHeight / 4))
  52.                     iPolyline.AddGeometry(iSegmentCollection)
  53.                     iGeometry = iPolyline
  54.             End Select
  55.         ElseIf TypeOf (iSymbol) Is ESRI.ArcGIS.Display.IFillSymbol Then
  56.             iSegmentCollection = New ESRI.ArcGIS.Geometry.Ring
  57.             iPolygon = New ESRI.ArcGIS.Geometry.Polygon
  58.             Select Case iStyle
  59.                 Case 0
  60.                     iSegmentCollection.AddSegment(CreateLine(5, iHeight - 5, iWidth - 6, iHeight - 5))
  61.                     iSegmentCollection.AddSegment(CreateLine(iWidth - 6, iHeight - 5, iWidth - 6, 6))
  62.                     iSegmentCollection.AddSegment(CreateLine(iWidth - 6, 6, 5, 6))
  63.                     iRing = iSegmentCollection
  64.                     iRing.Close()
  65.                     iPolygon.AddGeometry(iSegmentCollection)
  66.                     iGeometry = iPolygon
  67.             End Select
  68.         ElseIf TypeOf (iSymbol) Is ESRI.ArcGIS.Display.ISimpleTextSymbol Then
  69.             Select Case iStyle
  70.                 Case 0
  71.                     iPoint = New ESRI.ArcGIS.Geometry.Point
  72.                     iPoint.PutCoords(iWidth / 2, iHeight / 2)
  73.                     iGeometry = iPoint
  74.             End Select
  75.         End If
  76.         '绘制图形
  77.         iSymbol.SetupDC(iHDC, iDisplayTransformation)
  78.         iSymbol.Draw(iGeometry)
  79.         iSymbol.ResetDC()
  80.         iGraphics.ReleaseHdc(iHDC)
  81.         iGraphics.Dispose()
  82.         SymbolToBitmap = iBitmap
  83.     End Function
复制代码
过程基本就完成了,应该说没什么难度

未命名.JPG

上一篇:ArcOjects 3D开发方法简介

下一篇:ArcGis Engine空间插值方法