Setting the Color of a TextSymbol

2279
7
11-16-2011 06:50 AM
GeorgeFaraj
Occasional Contributor III
  
public void DrawText( IActiveView view, string text, string fontName, int fontColor, float fontSize, IPoint point ) {          
    ITextSymbol textSymbol = new TextSymbol();          
    stdole.IFontDisp font = textSymbol.Font;         
    font.Name = fontName;         
    font.Size = (decimal)fontSize;         
    textSymbol.Font = font;          
    IColor color = textSymbol.Color;         
    color.RGB = fontColor;         
    textSymbol.Color = color;          
    ITextElement textElement = new TextElementClass();         
    textElement.Text = text;         
    textElement.Symbol = textSymbol;         
    textElement.ScaleText = true;          
    IElement element = textElement as IElement;         
    element.Geometry = point;          
    view.GraphicsContainer.AddElement( element, 0 );          
    view.PartialRefresh( esriViewDrawPhase.esriViewGraphics, element, null );     
}


Can anyone suggest to me why this code always sets the font and size correctly but sometimes fails to set the color correctly? I always see the correct color in the 'textSymbol.Color' property after the line that sets it to 'color.' On the map (within a layer) it sometimes appears to 'combine' with that layer's fill color and off the map it sometimes appears 'blue.'  Is this a bug? I am using ArcGIS 10, service pack 2.
0 Kudos
7 Replies
JeffreyHamblin
New Contributor III
I don't have time right now to test this, but try the following:

Replace the line:
ITextSymbol textSymbol = new TextSymbol();
with:
ISimpleTextSymbol textSymbol = new TextSymbolClass();

and replace:
IColor color = textSymbol.Color;
with:
IRgbColor color = new RgbColorClass();
0 Kudos
NeilClemmons
Regular Contributor III
  
public void DrawText( IActiveView view, string text, string fontName, int fontColor, float fontSize, IPoint point ) {          
    ITextSymbol textSymbol = new TextSymbol();          
    stdole.IFontDisp font = textSymbol.Font;         
    font.Name = fontName;         
    font.Size = (decimal)fontSize;         
    textSymbol.Font = font;          
    IColor color = textSymbol.Color;         
    color.RGB = fontColor;         
    textSymbol.Color = color;          
    ITextElement textElement = new TextElementClass();         
    textElement.Text = text;         
    textElement.Symbol = textSymbol;         
    textElement.ScaleText = true;          
    IElement element = textElement as IElement;         
    element.Geometry = point;          
    view.GraphicsContainer.AddElement( element, 0 );          
    view.PartialRefresh( esriViewDrawPhase.esriViewGraphics, element, null );     
}


Can anyone suggest to me why this code always sets the font and size correctly but sometimes fails to set the color correctly? I always see the correct color in the 'textSymbol.Color' property after the line that sets it to 'color.' On the map (within a layer) it sometimes appears to 'combine' with that layer's fill color and off the map it sometimes appears 'blue.'  Is this a bug? I am using ArcGIS 10, service pack 2.


Is this on a 64-bit machine?  I've seen problems on 64-bit machines where apparently the alpha bit was not being read correctly.  I believe the solution was to set the transparency property on the color object to 1 instead of leaving it at the default of 0.
0 Kudos
GeorgeFaraj
Occasional Contributor III
I don't have time right now to test this, but try the following:


I'll freely admit that they way you suggested should be better but it exhibits the exact same behavior.
0 Kudos
GeorgeFaraj
Occasional Contributor III
Is this on a 64-bit machine?


I wish! (I run as fast as I can on the wheel to keep this thing in front of me going.)

And you meant 255, not 1, right? (My color selector is not using transparency so it was already 255.) Tried 0, 1, 255, and several other values and other than 0 not showing anything at all (as expected) the same behavior occurs (works and fails randomly.)
0 Kudos
JeffreyHamblin
New Contributor III
Charles,

If you provide some information regarding the values you are passing to your method, I will try to reproduce the issue. Either attach a zipped shapefile of the layer, or a description of it from which to create a dummy.

Is this used in an Add-In?
0 Kudos
GeorgeFaraj
Occasional Contributor III
If you provide some information regarding the values you are passing to your method, I will try to reproduce the issue.


Got it. I hope. I was converting the System.Drawing.Color that a control was giving me using ToArgb() instead of the function I had already written to extract all the pieces separately:

    public static int ConvertColorToRGB( System.Drawing.Color sourceColor ) {
        IRgbColor targetColor = new RgbColorClass();
        targetColor.Red = sourceColor.R;
        targetColor.Green = sourceColor.G;
        targetColor.Blue = sourceColor.B;
        targetColor.Transparency = sourceColor.A;
        return targetColor.RGB;
    }


Why should there be a difference between System.Drawing.Color.ToArgb() and the preceding code?
0 Kudos
JeffreyHamblin
New Contributor III

Why should there be a difference between System.Drawing.Color.ToArgb() and the preceding code?


The byte-ordering of the 32-bit ARGB value produced by System.Drawing.Color.ToArgb() is AARRGGBB.

The order for IColor.RGB is 00BBGGRR. The 00 byte is not used. So not only is the order reversed, but there is no Alpha (transparency) in the ESRI integer color.

So you will get very different colors assigning one to the other.
0 Kudos