4

I've created a function which sets the IColor to a specific ILayer, the value of transparency is between 0 and 255. When I set the value to 0, it gives me a transparent layer which is OK, but when I set it to 1,2,3,4,5,....255 it gives me the color as if it has 255 transparency. I don't understand, what's wrong with my code:

public static void setLayerColor(ILayer layer, IColor fillColor, IColor outlineColor, short width = 1)
    {if (layer == null || fillColor == null || outlineColor == null) return;
     IGeoFeatureLayer geoFeatureLayer = (IGeoFeatureLayer)layer;
     ISimpleRenderer simpleRenderer = (ISimpleRenderer)geoFeatureLayer.Renderer;

     ISimpleFillSymbol symbol = new SimpleFillSymbolClass();
     ILineSymbol lineSymbol = new SimpleLineSymbolClass();

     lineSymbol.Width = width;
     lineSymbol.Color = outlineColor;

     symbol.Color = fillColor;
     symbol.Outline = lineSymbol;
     simpleRenderer.Symbol = (ISymbol)symbol;}

My function has 4 parameters:

  • ILayer, the layer of which I want to change its color.

  • IColor, fillColor.

  • IColor, outlineColor.

  • short, the width of the outline.

5

I'm assuming that you're trying to set the IColor as RGBA with the transparency in the colour... that's not how it works, when you create an IColor from RGBA only the RGB is set, the alpha is ignored... see IColor, the options are CMYK, Gray, HLS, HSV, RGB - none of these have Alpha.

To get transparency on a layer you need to use the ILayerEffects Interface and specifically the Transparency property - be sure to check first if the layer type supports transparency before trying to use that property.

Assuming that your layer is a feature layer (you're creating a polygon symbol so it's a good bet) you need to set the Renderer (IFeatureRenderer) which is available from IGeoFeatureLayer which is implemented by IFeatureLayer.

simpleRenderer.Symbol = (ISymbol)symbol;  //last line of your code
IFeatureLayer AsFeatureLayer = (IFeatureLayer)layer;
IGeoFeatureLayer AsGeoFeatureLayer = (IGeoFeatureLayer) AsFeatureLayer;
AsGeoFeatureLayer.Renderer = (IFeatureRenderer)simpleRenderer; // set the renderer to the layer
ILayerEffects AsLayerEffects = (ILayerEffects) AsFeatureLayer;
AsLayerEffects.Transparency = 50; // 50% transparent

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.