0

I tried the following code the get the polygon color, but it returns the same color (0, 0, 255) for all polygons. How can I get the color of a polygon with ArcObjects?

        // Get polygon.
        IGeometry geometry = feature.Shape;
        IPolygon4 polygon = (IPolygon4)geometry;

        // Get color.
        IElement element = new PolygonElementClass();
        element.Geometry = feature.Shape;

        IFillShapeElement fillElement = element as IFillShapeElement;
        IFillSymbol fillSymbol = fillElement.Symbol;
        IRgbColor color = fillSymbol.Color as IRgbColor;

        MessageBox.Show(color.Red.ToString() + " " + color.Green.ToString() + " " + color.Blue.ToString());

I also tried to retrieve an element from IGraphicsContainer as:

            IEnvelope2 envelope = polygon.Envelope as IEnvelope2;

            IGraphicsContainer graphicsContainer = map as IGraphicsContainer;

            IEnumElement enumElement = graphicsContainer.LocateElementsByEnvelope(envelope);

            enumElement.Reset();
            IElement element = enumElement.Next();

But the obtained enumElement is null. Any ideas why?

1

Finally, I made it using IFeatureRenderer as:

            IGeoFeatureLayer geoGeatureLayer = featureLayer as IGeoFeatureLayer;
            IFeatureRenderer featureRenderer = geoGeatureLayer.Renderer;
            ISymbol symbol = featureRenderer.SymbolByFeature[feature];

            IFillSymbol fillSymbol = symbol as IFillSymbol;

            IColor color = fillSymbol.Color;

            IRgbColor rgbColor = new RgbColor();
            rgbColor.RGB = color.RGB;

            MessageBox.Show(rgbColor.Red.ToString() + " " + rgbColor.Green.ToString() + " " + rgbColor.Blue.ToString());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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