This document is archived and information here might be outdated.  Recommended version.


Add Graphic to Map Snippet (ArcObjects .NET 10.6 SDK)
ArcObjects Library Reference

Add Graphic to Map Snippet

Draw a specified graphic on the map using the supplied colors.

[C#]
///<summary>Draw a specified graphic on the map using the supplied colors.</summary>
///      
///<param name="map">An IMap interface.</param>
///<param name="geometry">An IGeometry interface. It can be of the geometry type: esriGeometryPoint, esriGeometryPolyline, or esriGeometryPolygon.</param>
///<param name="rgbColor">An IRgbColor interface. The color to draw the geometry.</param>
///<param name="outlineRgbColor">An IRgbColor interface. For those geometry's with an outline it will be this color.</param>
///      
///<remarks>Calling this function will not automatically make the graphics appear in the map area. Refresh the map area after after calling this function with Methods like IActiveView.Refresh or IActiveView.PartialRefresh.</remarks>
public void AddGraphicToMap(ESRI.ArcGIS.Carto.IMap map, ESRI.ArcGIS.Geometry.IGeometry geometry, ESRI.ArcGIS.Display.IRgbColor rgbColor, ESRI.ArcGIS.Display.IRgbColor outlineRgbColor)
{
  ESRI.ArcGIS.Carto.IGraphicsContainer graphicsContainer=(ESRI.ArcGIS.Carto.IGraphicsContainer)map; // Explicit Cast
  ESRI.ArcGIS.Carto.IElement element=null;
  if ((geometry.GeometryType) == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
  {
    // Marker symbols
    ESRI.ArcGIS.Display.ISimpleMarkerSymbol simpleMarkerSymbol=new ESRI.ArcGIS.Display.SimpleMarkerSymbolClass();
    simpleMarkerSymbol.Color=rgbColor;
    simpleMarkerSymbol.Outline=true;
    simpleMarkerSymbol.OutlineColor=outlineRgbColor;
    simpleMarkerSymbol.Size=15;
    simpleMarkerSymbol.Style=ESRI.ArcGIS.Display.esriSimpleMarkerStyle.esriSMSCircle;

    ESRI.ArcGIS.Carto.IMarkerElement markerElement=new ESRI.ArcGIS.Carto.MarkerElementClass();
    markerElement.Symbol=simpleMarkerSymbol;
    element=(ESRI.ArcGIS.Carto.IElement)markerElement; // Explicit Cast
  }
  else if ((geometry.GeometryType) == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
  {
    //  Line elements
    ESRI.ArcGIS.Display.ISimpleLineSymbol simpleLineSymbol=new ESRI.ArcGIS.Display.SimpleLineSymbolClass();
    simpleLineSymbol.Color=rgbColor;
    simpleLineSymbol.Style=ESRI.ArcGIS.Display.esriSimpleLineStyle.esriSLSSolid;
    simpleLineSymbol.Width=5;

    ESRI.ArcGIS.Carto.ILineElement lineElement=new ESRI.ArcGIS.Carto.LineElementClass();
    lineElement.Symbol=simpleLineSymbol;
    element=(ESRI.ArcGIS.Carto.IElement)lineElement; // Explicit Cast
  }
  else if ((geometry.GeometryType) == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
  {
    // Polygon elements
    ESRI.ArcGIS.Display.ISimpleFillSymbol simpleFillSymbol=new ESRI.ArcGIS.Display.SimpleFillSymbolClass();
    simpleFillSymbol.Color=rgbColor;
    simpleFillSymbol.Style=ESRI.ArcGIS.Display.esriSimpleFillStyle.esriSFSForwardDiagonal;
    ESRI.ArcGIS.Carto.IFillShapeElement fillShapeElement=new ESRI.ArcGIS.Carto.PolygonElementClass();
    fillShapeElement.Symbol=simpleFillSymbol;
    element=(ESRI.ArcGIS.Carto.IElement)fillShapeElement; // Explicit Cast
  }
  if (!(element == null))
  {
    element.Geometry=geometry;
    graphicsContainer.AddElement(element, 0);
  }
}
[Visual Basic .NET]
'''<summary>Draw a specified graphic on the map using the supplied colors.</summary>
'''      
'''<param name="map">An IMap interface.</param>
'''<param name="geometry">An IGeometry interface. It can be of the geometry type: esriGeometryPoint, esriGeometryPolyline, or esriGeometryPolygon.</param>
'''<param name="rgbColor">An IRgbColor interface. The color to draw the geometry.</param>
'''<param name="outlineRgbColor">An IRgbColor interface. For those geometry's with an outline it will be this color.</param>
'''      
'''<remarks></remarks>
Public Sub AddGraphicToMap(ByVal map As ESRI.ArcGIS.Carto.IMap, ByVal geometry As ESRI.ArcGIS.Geometry.IGeometry, ByVal rgbColor As ESRI.ArcGIS.Display.IRgbColor, ByVal outlineRgbColor As ESRI.ArcGIS.Display.IRgbColor)

  Dim graphicsContainer As ESRI.ArcGIS.Carto.IGraphicsContainer=CType(map, ESRI.ArcGIS.Carto.IGraphicsContainer) ' Explicit Cast
  Dim element As ESRI.ArcGIS.Carto.IElement=Nothing

  If (geometry.GeometryType)=ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint Then

    ' Marker symbols
    Dim simpleMarkerSymbol As ESRI.ArcGIS.Display.ISimpleMarkerSymbol=New ESRI.ArcGIS.Display.SimpleMarkerSymbolClass()
    simpleMarkerSymbol.Color=rgbColor
    simpleMarkerSymbol.Outline=True
    simpleMarkerSymbol.OutlineColor=outlineRgbColor
    simpleMarkerSymbol.Size=15
    simpleMarkerSymbol.Style=ESRI.ArcGIS.Display.esriSimpleMarkerStyle.esriSMSCircle

    Dim markerElement As ESRI.ArcGIS.Carto.IMarkerElement=New ESRI.ArcGIS.Carto.MarkerElementClass()
    markerElement.Symbol=simpleMarkerSymbol
    element=CType(markerElement, ESRI.ArcGIS.Carto.IElement) ' Explicit Cast

  ElseIf (geometry.GeometryType)=ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline Then

    '  Line elements
    Dim simpleLineSymbol As ESRI.ArcGIS.Display.ISimpleLineSymbol=New ESRI.ArcGIS.Display.SimpleLineSymbolClass()
    simpleLineSymbol.Color=rgbColor
    simpleLineSymbol.Style=ESRI.ArcGIS.Display.esriSimpleLineStyle.esriSLSSolid
    simpleLineSymbol.Width=5

    Dim lineElement As ESRI.ArcGIS.Carto.ILineElement=New ESRI.ArcGIS.Carto.LineElementClass()
    lineElement.Symbol=simpleLineSymbol
    element=CType(lineElement, ESRI.ArcGIS.Carto.IElement) ' Explicit Cast

  ElseIf (geometry.GeometryType)=ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon Then

    ' Polygon elements
    Dim simpleFillSymbol As ESRI.ArcGIS.Display.ISimpleFillSymbol=New ESRI.ArcGIS.Display.SimpleFillSymbolClass()
    simpleFillSymbol.Color=rgbColor
    simpleFillSymbol.Style=ESRI.ArcGIS.Display.esriSimpleFillStyle.esriSFSForwardDiagonal
    Dim fillShapeElement As ESRI.ArcGIS.Carto.IFillShapeElement=New ESRI.ArcGIS.Carto.PolygonElementClass()
    fillShapeElement.Symbol=simpleFillSymbol
    element=CType(fillShapeElement, ESRI.ArcGIS.Carto.IElement) ' Explicit Cast

  End If

  If Not(element Is Nothing) Then

        element.Geometry=geometry
        graphicsContainer.AddElement(element, 0)

  End If

End Sub

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.Carto
  • ESRI.ArcGIS.Display
  • ESRI.ArcGIS.Geometry
  • ESRI.ArcGIS.System