HOW TO

Change the fill and outline color of features in an ArcGIS Server feature service with an ArcMap add-in

Last Published: April 25, 2020

Summary

This code sample shows how to change the fill and outline color of features in an ArcGIS Server feature service using an ArcMap add-in. The changes can persist in ArcMap by saving the ArcMap document.

Procedure

The code below shows how to change the fill and outline color of polygon features in an ArcGIS Server Feature Service.

Sample Code (C#)
 using System;
 using System.Collections.Generic;
 using System.Text;
 using System.IO;
 using ESRI.ArcGIS.esriSystem;
 using ESRI.ArcGIS.ArcMapUI;
 using ESRI.ArcGIS.Geodatabase;
 using ESRI.ArcGIS.Carto;
 using ESRI.ArcGIS.Geometry;
 using ESRI.ArcGIS.Display;

 namespace Add_FeatureService_to_ArcMap
 {
     public class Add_FeatureService_to_ArcMap : ESRI.ArcGIS.Desktop.AddIns.Button
     {
         public Add_FeatureService_to_ArcMap()
         {
         }

         protected override void OnClick()
         {
             IMxDocument pMx = ArcMap.Application.Document as IMxDocument;
             //You may replace this url with your own feature service url. In my case, I have an ArcGIS Server feature service that contains polygon features at the URL shown below:
             string url = "http://localhost:6080/arcgis/rest/services/FC1_Polygons/FeatureServer";
             IPropertySet pFeatServProp = new PropertySet();
             pFeatServProp.SetProperty("DATABASE", url);
             IWorkspaceFactory pFeatWorkspaceFact = new FeatureServiceWorkspaceFactory() as IWorkspaceFactory;
             IFeatureWorkspace pFeatureWorkspace = pFeatWorkspaceFact.Open(pFeatServProp, 0) as IFeatureWorkspace;
             IFeatureClass pfeatClass = pFeatureWorkspace.OpenFeatureClass("0") as IFeatureClass;
             // "0" corresponds to the first layer with index/id 0, similarly, "1", "2" refer to layer id 1
             // and id 2, you may check the service rest end point to have a valid parameter in the OpenFeatureClass.

             IFeatureLayer pfeatLayer = new FeatureLayer();
             pfeatLayer.FeatureClass = pfeatClass;
             pfeatLayer.Name = "Sami_FC1";
             IGroupLayer pGroupLayer = new GroupLayer();
             pGroupLayer.Name = "GroupLayerName";

             // -------------------------------- RENDERER ---------------------------------------------------  
             // Creating RGB Colors by pulling values from a Microsoft Color object
             System.Drawing.Color c = System.Drawing.Color.FromName("SlateBlue");
             IRgbColor rgbColor_Fill = CreateRgbColor(c);

             System.Drawing.Color c2 = System.Drawing.Color.FromName("Red");
             IRgbColor rgbColor_Outline = CreateRgbColor(c2);

             ISimpleLineSymbol simpleLineSymbol_Outline = new SimpleLineSymbol();
             simpleLineSymbol_Outline.Color = rgbColor_Outline;
             simpleLineSymbol_Outline.Width = 3.0;

             ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbol();
             simpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
             simpleFillSymbol.Color = rgbColor_Fill;
             simpleFillSymbol.Outline = simpleLineSymbol_Outline;

             ISimpleRenderer simpleRenderer = new SimpleRenderer();
             simpleRenderer.Label = "Simple Renderer";
             simpleRenderer.Symbol = simpleFillSymbol as ISymbol;
             IFeatureRenderer featureRenderer = simpleRenderer as IFeatureRenderer;

             IGeoFeatureLayer geoFeatureLayer = pfeatLayer as IGeoFeatureLayer;
             geoFeatureLayer.Renderer = featureRenderer;

             //--------------------------------  END  RENDERER ---------------------------------------------------

             pGroupLayer.Add(pfeatLayer);
             pMx.FocusMap.AddLayer(pGroupLayer);

             ArcMap.Application.CurrentTool = null;
         }
         // Method used to extract RGB components out of the Microsoft Color object (a structure)
         protected static IRgbColor CreateRgbColor(System.Drawing.Color c)
         {
             // Note that c is a Microsoft color object, which has 4 components: alpha, R, G, B
             // The R, G, B components of the System.Drawing.Color object are assigned to the the ArcObjects IRgbColor object
             IRgbColor rgb = new RgbColorClass();
             rgb.Red = c.R; rgb.Green = c.G; rgb.Blue = c.B;
             rgb.UseWindowsDithering = false;
             return rgb;
         }
     }
}

Article ID:000013843

Software:
  • ArcMap
  • AO SDK Microsoft NET Framework 10 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic