Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers 2

up vote 6 down vote accepted

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();  
share|improve this answer
    
+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. –  Kirk Kuykendall Jan 13 '11 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 Jan 13 '11 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 Jan 13 '11 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. –  Kirk Kuykendall Jan 13 '11 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 Jan 13 '11 at 23:50
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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