ArcGIS Add graphics
ArcGIS_AddGraphics_CSharp\App_Code\PolygonTool.cs
// Copyright 2011 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 

using System;
public class PolygonTool : ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction
{
  #region IMapServerToolAction Members

  public void ServerAction(ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolEventArgs toolEventArgs)
  {
    // Get a reference to the Map control on which the tool was executed
    ESRI.ArcGIS.ADF.Web.UI.WebControls.Map adfMap = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)toolEventArgs.Control;

    // Get the polygon drawn by the user
    ESRI.ArcGIS.ADF.Web.UI.WebControls.MapPolygonEventArgs mapPolygonEventArgs = (ESRI.ArcGIS.ADF.Web.UI.WebControls.MapPolygonEventArgs)toolEventArgs;
    ESRI.ArcGIS.ADF.Web.Geometry.Polygon mapPolygon = mapPolygonEventArgs.MapPolygon;

    // Convert the Web ADF input polygon to its ArcGIS Server data source specific equivalent
    ESRI.ArcGIS.ADF.ArcGISServer.PolygonN agsSoapPolygon = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromAdfPolygon(mapPolygon);

    // Get an ArcGIS Server specific map functionality.  Note this code assumes the existence of an ArcGIS
    // Server resource item with the name "MapResourceItem0"
    ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality agsMapFunctionality = (ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality)(adfMap.GetFunctionality("MapResourceItem0"));

    // Get the ArcGIS Server MapDescription for the resource
    ESRI.ArcGIS.ADF.ArcGISServer.MapDescription agsSoapMapDescription = agsMapFunctionality.MapDescription;

    // Create a yellow ArcGIS Server data source specific color object
    ESRI.ArcGIS.ADF.ArcGISServer.RgbColor agsSoapRgbColor = new ESRI.ArcGIS.ADF.ArcGISServer.RgbColor();
    agsSoapRgbColor.Red = 255;
    agsSoapRgbColor.Green = 255;
    agsSoapRgbColor.Blue = 0;

    // Initialize an ArcGIS Server data source specific fill symbol with a solid fill and the color initialized above
    ESRI.ArcGIS.ADF.ArcGISServer.SimpleFillSymbol agsSoapSimpleFillSymbol = new ESRI.ArcGIS.ADF.ArcGISServer.SimpleFillSymbol();
    agsSoapSimpleFillSymbol.Style = ESRI.ArcGIS.ADF.ArcGISServer.esriSimpleFillStyle.esriSFSSolid;
    agsSoapSimpleFillSymbol.Color = agsSoapRgbColor;

    // Create an ArcGIS Server data source specific polygon element with the fill symbol and input polygon
    ESRI.ArcGIS.ADF.ArcGISServer.PolygonElement agsSoapPolygonElement = new ESRI.ArcGIS.ADF.ArcGISServer.PolygonElement();
    agsSoapPolygonElement.Symbol = agsSoapSimpleFillSymbol;
    agsSoapPolygonElement.Polygon = agsSoapPolygon;

    // If the current resource does not have any custom graphics specified, initialize this property with
    // a new array with just the newly created graphic element.  Otherwise, create a new array with both
    // the current custom graphics and teh newly created element.
    if (agsSoapMapDescription.CustomGraphics != null)
    {
      // Create a new ArcGIS Server graphic element array with a size that is one bigger than the
      // resource's current element array
      int elementCount = agsSoapMapDescription.CustomGraphics.Length;
      ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[] agsSoapGraphicElementArray = new ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[elementCount + 1];

      // Copy the resource's current element graphic array to the new array
      agsSoapMapDescription.CustomGraphics.CopyTo(agsSoapGraphicElementArray, 0);

      // Add the newly created polygon element to the array
      agsSoapGraphicElementArray[elementCount] = agsSoapPolygonElement;

      // Update the resource's element array with the new array
      agsSoapMapDescription.CustomGraphics = agsSoapGraphicElementArray;
    }
    else
    {
      // Create a new ArcGIS Server graphic element array with only the newly created polygon element
      ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[] agsSoapGraphicElement = new ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[1];
      agsSoapGraphicElement[0] = agsSoapPolygonElement;

      // Update the resource's element array with the new array
      agsSoapMapDescription.CustomGraphics = agsSoapGraphicElement;
    }

    // Refresh the resource so the new polygon is shown
    adfMap.RefreshResource(agsMapFunctionality.Resource.Name);
  }

  #endregion
}