当我们用Symbologycontrol设置某个图层symbol的样式或设置指北针、比例尺的样式时,我们需要将这些选中的样式以图片的形式显示在例如PictureBox中,这时我们就需要得到选中对象的Image,下面是我提供的两种方法:
一:是利用AE自带的PreviewItem方法来实现,这种方法普适性好,能适用所有Symbolgycontrol支持的对象
二:是利用Symbol的Draw函数实现,该方法目前只支持Symbol,可以进行扩充
方法一:
''' <summary>
''' 通过SymbolObject获得image
''' </summary>
''' <param name="pStyleGallery">传入的Symbologycontrol中选中的项</param>
''' <param name="pStyleClass">当前类型</param>
''' <param name="pWidth">图像宽度</param>
''' <param name="pHeight">图像高度</param>
''' <returns>得到的Image</returns>
''' <remarks></remarks>
Shared Function GetImageFromSymbolObject(ByVal pStyleGallery As IStyleGalleryItem, ByVal pStyleClass As esriSymbologyStyleClass, ByVal pWidth As Double, ByVal pHeight As Double) As Image
Dim pSymbology As ISymbologyControl = New SymbologyControl
pSymbology.StyleClass = pStyleClass
pSymbology.GetStyleClass(pSymbology.StyleClass).AddItem(pStyleGallery, 0)
Dim picture As stdole.IPictureDisp = pSymbology.GetStyleClass(pSymbology.StyleClass).PreviewItem(pStyleGallery, pWidth, pHeight)
Dim image As System.Drawing.Image = System.Drawing.Image.FromHbitmap(New System.IntPtr(picture.Handle))
Return image
End Function
''' <summary>
''' 通过SymbolObject获得image
''' </summary>
''' <param name="pSymbology">传入的symbologycontrol</param>
''' <param name="pStyleGallery">传入的Symbologycontrol中选中的项</param>
''' <param name="pWidth">图像宽度</param>
''' <param name="pHeight">图像高度</param>
''' <returns>得到的Image</returns>
''' <remarks></remarks>
Shared Function GetImageFromSymbolObject(ByVal pSymbology As ISymbologyControl, ByVal pStyleGallery As IStyleGalleryItem, ByVal pWidth As Double, ByVal pHeight As Double) As Image
Dim picture As stdole.IPictureDisp = pSymbology.GetStyleClass(pSymbology.StyleClass).PreviewItem(pStyleGallery, pWidth, pHeight)
Dim image As System.Drawing.Image = System.Drawing.Image.FromHbitmap(New System.IntPtr(picture.Handle))
Return image
End Function
''' <summary>
''' 通过SymbolObject获得image
''' </summary>
''' <param name="pStyleClass">当前的类型</param>
''' <param name="pSymbolObject">传入的对象,比如TextSymbol,NorthArrow</param>
''' <param name="pWidth">图像宽度</param>
''' <param name="pHeight">图像高度</param>
''' <returns>得到的Image</returns>
''' <remarks></remarks>
Shared Function GetImageFromSymbolObject(ByVal pStyleClass As esriSymbologyStyleClass, ByVal pSymbolObject As Object, ByVal pWidth As Double, ByVal pHeight As Double) As Image
Dim pSymbology As ISymbologyControl = New SymbologyControl
Dim pStyleGallery As IStyleGalleryItem = New ServerStyleGalleryItem
pStyleGallery.Name = "当前符号"
pStyleGallery.Item = pSymbolObject
pSymbology.GetStyleClass(pStyleClass).AddItem(pStyleGallery, 0)
Dim picture As stdole.IPictureDisp = pSymbology.GetStyleClass(pStyleClass).PreviewItem(pStyleGallery, pWidth, pHeight)
Dim image As System.Drawing.Image = System.Drawing.Image.FromHbitmap(New System.IntPtr(picture.Handle))
Return image
End Function
''' <summary>
''' 通过SymbolObject获得image
''' </summary>
''' <param name="pSymbolObject">传入的对象,比如TextSymbol,NorthArrow</param>
''' <param name="pWidth">图像宽度</param>
''' <param name="pHeight">图像高度</param>
''' <returns>得到的Image</returns>
''' <remarks></remarks>
Shared Function GetImageFromSymbolObject(ByVal pSymbolObject As Object, ByVal pWidth As Double, ByVal pHeight As Double) As Image
Dim pSymbology As ISymbologyControl = New SymbologyControl
Dim pStyleGallery As IStyleGalleryItem = New ServerStyleGalleryItem
pStyleGallery.Name = "当前符号"
pStyleGallery.Item = pSymbolObject
Dim pStyleClass As esriSymbologyStyleClass = GetStyleClass(pSymbolObject)
pSymbology.GetStyleClass(pStyleClass).AddItem(pStyleGallery, 0)
Dim picture As stdole.IPictureDisp = pSymbology.GetStyleClass(pStyleClass).PreviewItem(pStyleGallery, pWidth, pHeight)
Dim image As System.Drawing.Image = System.Drawing.Image.FromHbitmap(New System.IntPtr(picture.Handle))
Return image
End Function
''' <summary>
''' 获得当前对应的styleclass
''' </summary>
''' <param name="pSymbolObject">当前的对象</param>
''' <returns></returns>
''' <remarks></remarks>
Shared Function GetStyleClass(ByVal pSymbolObject As Object) As esriSymbologyStyleClass
If TypeOf pSymbolObject Is ITextSymbol Then
Return esriSymbologyStyleClass.esriStyleClassTextSymbols
ElseIf TypeOf pSymbolObject Is IMarkerSymbol Then
Return esriSymbologyStyleClass.esriStyleClassMarkerSymbols
ElseIf TypeOf pSymbolObject Is ILineSymbol Then
Return esriSymbologyStyleClass.esriStyleClassLineSymbols
ElseIf TypeOf pSymbolObject Is IFillSymbol Then
Return esriSymbologyStyleClass.esriStyleClassFillSymbols
ElseIf TypeOf pSymbolObject Is IScaleText Then
Return esriSymbologyStyleClass.esriStyleClassScaleTexts
ElseIf TypeOf pSymbolObject Is IScaleBar Then
Return esriSymbologyStyleClass.esriStyleClassScaleBars
ElseIf TypeOf pSymbolObject Is INorthArrow Then
Return esriSymbologyStyleClass.esriStyleClassNorthArrows
ElseIf TypeOf pSymbolObject Is IBackground Then
Return esriSymbologyStyleClass.esriStyleClassBackgrounds
ElseIf TypeOf pSymbolObject Is IBorder Then
Return esriSymbologyStyleClass.esriStyleClassBorders
ElseIf TypeOf pSymbolObject Is IShadow Then
Return esriSymbologyStyleClass.esriStyleClassLabels
ElseIf TypeOf pSymbolObject Is ILegend Then
Return esriSymbologyStyleClass.esriStyleClassLegendItems
End If
End Function
方法二:
''' <summary>
''' 将symbol转成bitmap
''' </summary>
''' <param name="pSymbol">symbol</param>
''' <param name="iwidth">图像宽</param>
''' <param name="iheight">图像高</param>
''' <returns>返回bitmap</returns>
''' <remarks></remarks>
Shared Function SymbolToBitmp(ByVal pSymbol As ISymbol, ByVal iwidth As Integer, ByVal iheight As Integer) As System.Drawing.Bitmap
''根据高宽创建图象
Dim bmp As Drawing.Bitmap = New Drawing.Bitmap(iwidth, iheight)
Dim gImage As Drawing.Graphics = Drawing.Graphics.FromImage(bmp)
gImage.Clear(Drawing.Color.White)
Dim dpi As Double = gImage.DpiX
Dim pEnvelope As IEnvelope = New EnvelopeClass()
pEnvelope.PutCoords(0, 0, bmp.Width, bmp.Height)
Dim deviceRect As New tagRECT
deviceRect.left = 0
deviceRect.right = bmp.Width
deviceRect.top = 0
deviceRect.bottom = bmp.Height
Dim pDisplayTransformation As IDisplayTransformation = New DisplayTransformationClass
pDisplayTransformation.VisibleBounds = pEnvelope
pDisplayTransformation.Bounds = pEnvelope
pDisplayTransformation.DeviceFrame = deviceRect
pDisplayTransformation.Resolution = dpi
Dim pGeo As IGeometry = CreateSymShape(pSymbol, pEnvelope)
Dim hdc As System.IntPtr = New IntPtr()
hdc = gImage.GetHdc()
''将符号的形状绘制到图象中
pSymbol.SetupDC(hdc, pDisplayTransformation)
pSymbol.Draw(pGeo)
pSymbol.ResetDC()
gImage.ReleaseHdc(hdc)
gImage.Dispose()
Return bmp
End Function
Shared Function CreateSymShape(ByVal pSymbol As ISymbol, ByVal pEnvelope As IEnvelope) As IGeometry
Dim pGeo As IGeometry = Nothing
'' 根据传入的符号以及外包矩形区域返回对应的几何空间实体(点,线、面)
''判断是否为“点”符号
If TypeOf pSymbol Is IMarkerSymbol Then
Dim pMarkerSym As IMarkerSymbol = pSymbol
If pMarkerSym IsNot Nothing Then
''为“点”符号则返回IEnvelope的中心点
Dim pArea As IArea = pEnvelope
pGeo = pArea.Centroid
End If
ElseIf TypeOf pSymbol Is ILineSymbol Then
''判断是否为“线”符号
Dim IlineSym As ILineSymbol = pSymbol
If IlineSym IsNot Nothing Then
''返回45度的对角线
Dim IpLine As IPolyline
IpLine = New PolylineClass()
IpLine.FromPoint = pEnvelope.LowerLeft
IpLine.ToPoint = pEnvelope.UpperRight
pGeo = IpLine
End If
ElseIf TypeOf pSymbol Is IFillSymbol Then
''直接返回一个IEnvelope矩形区域
pGeo = pEnvelope
End If
Return pGeo
End Function
标签:
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn
文章转载自:慧都控件网