ArcObjects 10.3 Add Transparent Polygon to Map

4882
13
Jump to solution
07-20-2015 07:56 AM
JoshuaCorcoran
New Contributor II

Hi everyone. This is an issue I have been trying to tackle for a while and decided to reach out for help. I am creating an ESRI ArcGIS Desktop Add-In that allows the user to draw a polygon and then have it added to the map. I am able to capture the polygon and add it to the map, the issue is the transparency. Currently and by default it is 100% opacity and solid. I want to make it around 50% opacity so the user can see the data behind it.

Here is the code I have so far:

public static void AddPolygonToMap(IActiveView ActiveViewInstance, IGeometry NewGeo)
        {
            //Local Variable Declaration
            var fillShapeElement = default(IFillShapeElement);
            var element = default(IElement);
            var graphicsContainer = default(IGraphicsContainer);
            var simpleFilleSymbol = default(ISimpleFillSymbol);
            var newRgbColor = default(IRgbColor);
            var lineSymbol = default(ILineSymbol);


            //Use the IElement interface to set the Envelope Element's geo
            element = new PolygonElement();
            element.Geometry = NewGeo;


            //QI for the IFillShapeElement interface so that the symbol property can be set
            fillShapeElement = element as IFillShapeElement;


            //Create a new fill symbol
            simpleFilleSymbol = new SimpleFillSymbol();


            //Create a new color marker symbol of the color black;
            newRgbColor = new RgbColor();
            newRgbColor.Red = 0;
            newRgbColor.Green = 0;
            newRgbColor.Blue = 0;
            
            //Create a new line symbol so that we can set the width outline
            lineSymbol = new SimpleLineSymbol();
            lineSymbol.Color = newRgbColor;
            lineSymbol.Width = 2;


            //Setup the Simple Fill Symbol
            simpleFilleSymbol.Color = newRgbColor;
            simpleFilleSymbol.Style = esriSimpleFillStyle.esriSFSHollow;
            simpleFilleSymbol.Outline = lineSymbol;
            fillShapeElement.Symbol = simpleFilleSymbol;


            //QI for the graphics container from the active view allows access to basic graphics layer
            graphicsContainer = ActiveViewInstance as IGraphicsContainer;


            //Add the new element at Z order 0
            graphicsContainer.AddElement((IElement)fillShapeElement, 0);


            //Show the new graphic
            ActiveViewInstance.Refresh();
        }

I know that this is possible somehow and I am sure it's just a line or two missing but any help would be much appreciated.

V/r,

Josh

0 Kudos
1 Solution

Accepted Solutions
DuncanHornby
MVP Notable Contributor

Joshua,

I wonder if the technique I pointed to expects your graphics to be simple fills? Below are two polygons one a simple blue fill the other a picture file.

Capture.PNG

I run the code as described in that link and I get below; so it works well with simple fill but not great with the picture fill.Capture.PNG

The code was:

Public Sub MakeTransparent()
    ' Get map document
    Dim pMXDoc As IMxDocument
    Set pMXDoc = ThisDocument

    ' Get Map
    Dim pMap As IMap
    Set pMap = pMXDoc.FocusMap

    ' Get Graphics Container for map
    Dim pGC As IGraphicsContainer
    Set pGC = pMap
    pGC.Reset

    ' Get first graphic, assumed to be polygon
    Dim pElement As IElement
    Set pElement = pGC.Next

    Dim rgb As IRgbColor
    Set rgb = New RgbColor
    rgb.Red = 255
   
    Do While Not pElement Is Nothing
        ' Get symbol of graphic and its colour
        Dim pFillShapeElement As IFillShapeElement
        Set pFillShapeElement = pElement
        Dim pFillSymbol As IFillSymbol
        Set pFillSymbol = pFillShapeElement.Symbol
        Dim pColour As IColor
        Set pColour = pFillSymbol.Color
        Dim psym As ISymbol
        Set psym = pFillSymbol
        psym.ROP2 = esriROPMaskPen
        pFillSymbol.Color = rgb
        pFillShapeElement.Symbol = pFillSymbol
        Set pElement = pGC.Next
    Loop
   
    ' Refresh map
    pMXDoc.ActiveView.Refresh
End Sub

View solution in original post

13 Replies
DuncanHornby
MVP Notable Contributor

Joshua,

Have a look at this thread which discusses this issue. In a link in the final comment to my answer is a solution that you are probably after.

Duncan

0 Kudos
JoshuaCorcoran
New Contributor II

Duncan thanks for the reply, I have actually been pointed to this post a couple times but unfortunately IColor only support a value of 0 or 255. So completely solid or completely transparent and I am looking for something in the 50% range.

DuncanHornby
MVP Notable Contributor

Ah but did you follow the link in the final comment...

0 Kudos
JoshuaCorcoran
New Contributor II

Ducan I did check out the link but all it seemed to be doing was casting the ISimpleFillSymbol to a ISymbol and setting the ROP2 property which did not affect my graphics at all.

0 Kudos
DuncanHornby
MVP Notable Contributor

Joshua,

I wonder if the technique I pointed to expects your graphics to be simple fills? Below are two polygons one a simple blue fill the other a picture file.

Capture.PNG

I run the code as described in that link and I get below; so it works well with simple fill but not great with the picture fill.Capture.PNG

The code was:

Public Sub MakeTransparent()
    ' Get map document
    Dim pMXDoc As IMxDocument
    Set pMXDoc = ThisDocument

    ' Get Map
    Dim pMap As IMap
    Set pMap = pMXDoc.FocusMap

    ' Get Graphics Container for map
    Dim pGC As IGraphicsContainer
    Set pGC = pMap
    pGC.Reset

    ' Get first graphic, assumed to be polygon
    Dim pElement As IElement
    Set pElement = pGC.Next

    Dim rgb As IRgbColor
    Set rgb = New RgbColor
    rgb.Red = 255
   
    Do While Not pElement Is Nothing
        ' Get symbol of graphic and its colour
        Dim pFillShapeElement As IFillShapeElement
        Set pFillShapeElement = pElement
        Dim pFillSymbol As IFillSymbol
        Set pFillSymbol = pFillShapeElement.Symbol
        Dim pColour As IColor
        Set pColour = pFillSymbol.Color
        Dim psym As ISymbol
        Set psym = pFillSymbol
        psym.ROP2 = esriROPMaskPen
        pFillSymbol.Color = rgb
        pFillShapeElement.Symbol = pFillSymbol
        Set pElement = pGC.Next
    Loop
   
    ' Refresh map
    pMXDoc.ActiveView.Refresh
End Sub
JoshuaCorcoran
New Contributor II

Duncan I have tried to implement the proposed solution using the following code but instead of making the graphic transparent it makes it completely disappear from the screen. I appears to be making it invisible.

            ESRI.ArcGIS.Display.IScreenDisplay screenDisplay = this.ActiveViewInstance.ScreenDisplay;


            // Constant
            screenDisplay.StartDrawing(screenDisplay.hDC, (System.Int16)ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache); // Explicit Cast
            ESRI.ArcGIS.Display.IRgbColor rgbColor = new ESRI.ArcGIS.Display.RgbColorClass();
            rgbColor.Red = 255;


            ESRI.ArcGIS.Display.IColor color = rgbColor; // Implicit Cast
            ESRI.ArcGIS.Display.ISimpleFillSymbol simpleFillSymbol = new ESRI.ArcGIS.Display.SimpleFillSymbolClass();
            simpleFillSymbol.Color = color;


            ESRI.ArcGIS.Display.ISymbol symbol = simpleFillSymbol as ESRI.ArcGIS.Display.ISymbol; // Dynamic Cast
            ESRI.ArcGIS.Display.IRubberBand rubberBand = new ESRI.ArcGIS.Display.RubberPolygonClass();
            ESRI.ArcGIS.Geometry.IGeometry geometry = rubberBand.TrackNew(screenDisplay, symbol);
            screenDisplay.SetSymbol(symbol);
            screenDisplay.DrawPolygon(geometry);
            screenDisplay.FinishDrawing();


            //Try Make Transparent
            var graphicsContainer = this.ActiveViewInstance.FocusMap as IGraphicsContainer;
            graphicsContainer.Reset();


            var element = graphicsContainer.Next();
            ESRI.ArcGIS.Display.IRgbColor rgbColor2 = new ESRI.ArcGIS.Display.RgbColor();
            rgbColor2.Red = 255;


            while (element != null)
            {
                var shapeFille = element as IFillShapeElement;
                var fillSymbol = shapeFille.Symbol as IFillSymbol;
                var iSymbol = fillSymbol as ISymbol;
                iSymbol.ROP2 = esriRasterOpCode.esriROPMaskPen;
                fillSymbol.Color = rgbColor2;
                shapeFille.Symbol = fillSymbol;
                element = graphicsContainer.Next();
            }


            this.ActiveViewInstance.Refresh();
0 Kudos
DuncanHornby
MVP Notable Contributor

Joshua,

Looking at the code nothing leaps out at me. The only thing I can think of is you are creating your graphic polygon via the RubberBand class. I did not do that I created the graphics then ran the code. Can you have an MXD with a drawn graphic on the map (so you are not using the RubberBand) to see if the code works for you. If that does then it suggests something is not happening when you create the graphic via the RubberBand method?

Duncan

0 Kudos
thejuskambi
Regular Contributor

IColor has a Transparency property which you can use.

0 Kudos
JoshuaCorcoran
New Contributor II

Thanks for the reply but unfortunately IColor only support a value of 0 or 255. So completely solid or completely transparent and I am looking for something in the 50% range.

0 Kudos