1

I have a ILayer which I load from a .lyr file which I created from a shapefile. How can I change the colors of the elements? I want to make the roads red instead of green for example.

I need to do this programatically with ArcEngine 10 and C# VS2010.

2 Answers 2

6

The layer's renderer can be accessed through IGeoFeatureLayer.Renderer.

At this point, it depends on which type of renderer the layer uses, e.g. whether it is a simple renderer, unique values renderer, or something else. For list of common renderers, take a look at which classes implement the IFeatureRenderer interface.

For example, if the layer uses simple renderer, you will change the color by assigning a new symbol through ISimpleRenderer.Symbol. The general symbol type (marker symbol, line symbol, fill symbol) is determined by the type of layer's geometry.

This sample code assumes a simple renderer assigned to a point layer (C#):

var renderer = (IGeoFeatureLayer)layer;
var simpleRender = renderer as ISimpleRenderer;
if (simpleRenderer == null) return;  // not a simple renderer

var symbol = simpleRenderer.Symbol;
var markerSymbol = symbol as IMarkerSymbol;
if (markerSymbol == null) return; // not a marker symbol, layer's geometry type is not point

markerSymbol.Color = new RgbColorClass { Red = 255, Green = 0, Blue = 0 }; // change to red
simpleRenderer.Symbol = symbol; // assign back to the renderer

activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, layer, null); // refresh the layer

// alternatively, do a full refresh
// activeView.Refresh();

// when in ArcMap, update the TOC view to reflect new symbology
mxDocument.UpdateContents();  
5
  • +1 good explanation. A common gotcha is sometimes you have what appears to be an ISimplemarkersymbol, when in fact it is an IMultiLayerMarkerSymbol with a single layer (which is in turn an ISimpleMarkerSymbol). So you might try casting to see if it's a multilayer symbol. I think the symbol browser typically returns multi layer symbols. Commented Jan 13, 2011 at 18:48
  • Good point, the symbol editor in my experience always returns a multilayer symbol. This is true for markers, lines and fills.
    – Petr Krebs
    Commented Jan 13, 2011 at 19:01
  • As a side note, not directly related to this - I have found that a simple symbol wrapped in a single-layer multilayer symbol draws noticeably slower than the simple symbol alone. Very weird.
    – Petr Krebs
    Commented Jan 13, 2011 at 19:03
  • hmmm, I wonder if using an msd as a mapservice instead of an mxd means multilayer symbols with a single layer get unpackaged. Commented Jan 13, 2011 at 23:44
  • I believe there is more to MSD than that, but who knows.. ESRI is talking about it in context of "new rendering engine", but I've never seen any detailed technical information how are the drawing optimizations done. I can only guess there are optimized algorithms for drawing most common symbol types, bypassing the classic ISymbol implementations.
    – Petr Krebs
    Commented Jan 13, 2011 at 23:50
3

Cast the ILayer to a IGeoFeatureLayer to access the Renderer, make sure that you refresh the activeview and toc after you changed the renderer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.