Select to view content in your preferred language

Change color of Esri 2d Symbols

483
3
04-12-2023 08:19 AM
Labels (3)
aweisASC
New Contributor

I'm trying to change the color of some of the ESRI 2d symbols available in the ArcGIS Portal. Below is my code. Is it possible to change the color of the returned symbols from the esri2DPointSymbolStyle.GetSymbolAsync call? If not is there a better solution to using and customizing the ArcGIS portal symbols or more information on how to use youre own icons?

 

private async Task CreateSymbology(FeatureLayer featureLayer, string symbolName)
{
// Create a portal to enable access to the symbols.
ArcGISPortal portal = await ArcGISPortal.CreateAsync();

// Create a SymbolStyle, this is used to return symbols based on provided symbol keys from portal.
SymbolStyle esri2DPointSymbolStyle = await SymbolStyle.OpenAsync("Esri2DPointSymbolsStyle", portal);

// This call is used to retrieve a single symbol for a given symbol name, if multiple symbol names are provided
// a multilayer symbol assembled and returned for the given symbol names.
Symbol symbol = await esri2DPointSymbolStyle.GetSymbolAsync(new List<string> { symbolName });

// Get the image source for the symbol to populate the legend UI.
RuntimeImage symbolSwatch = await symbol.CreateSwatchAsync();
ImageSource imageSource = await RuntimeImageExtensions.ToImageSourceAsync(symbolSwatch);

// Add the symbol the ObservableCollection containing the symbol legend data.
_symbolLegendCollection.Add(new SymbolLegendInfo() { Name = symbolName, ImageSource = imageSource });


SimpleRenderer sr = new SimpleRenderer(symbol);

featureLayer.Renderer = sr;
}

0 Kudos
3 Replies
dotMorten_esri
Esri Notable Contributor

You can inspect the symbol instance's properties and you should be able to change anything including colors (note that you might have to cast the symbol to a more concrete type depending on what you're getting back).

0 Kudos
AndrewWeisASC
New Contributor

The Symbol object has properties that can be changed. Here is an example on how to change the color of an esri2dPointSymbolStyle

 

// Get 2D Point Symbol

Symbol symbol = await esri2DPointSymbolStyle.GetSymbolAsync(new List<string> { "university" });


// Modify the color property of the symbol
Type symbolType = symbol.GetType();

PropertyInfo colorProperty = symbolType.GetProperty("Color");

Color desiredColor = Color.FromArgb(14, 153, 30);

colorProperty.SetValue(symbol, desiredColor);

 

// Add Symbol to Renderer

SimpleRenderer sr = new SimpleRenderer(symbol);

featureLayer.Renderer = sr;

 

 

Hope this helps future .Net SDK users!

0 Kudos
dotMorten_esri
Esri Notable Contributor

You can save all the reflection (which would fail on certain platforms in release builds), by just casting it to the proper CIM symbol type, and access the Color property directly in a type-safe way.

0 Kudos