C#Random类生成伪随机数,高频率使用会导致生成相同的随机序列。为避免渲染的图层的颜色一样,每次生成颜色需要使用不同的种子。
private static int m_RandomSeed = 0;
private static int RandomSeed
{
get
{
m_RandomSeed++;
if (m_RandomSeed == int.MaxValue)
m_RandomSeed = 0;
return m_RandomSeed % 20;
}
}
public void AddLayer(IMap TargetMap, IFeatureClass AddFeaCls, string LayerName = "",
bool IsSimpleRender = false, string DisplayField = "", bool IsLable = false)
{
IFeatureLayer pFeaLayer = new FeatureLayerClass();
pFeaLayer.FeatureClass = AddFeaCls;
pFeaLayer.Name = string.IsNullOrEmpty(LayerName) ? AddFeaCls.AliasName : LayerName;
if (IsSimpleRender && AddFeaCls.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
{
ISimpleRenderer simpleRender = new SimpleRenderer();
ISymbol result = new ESRI.ArcGIS.Display.SimpleFillSymbolClass();
(result as ISimpleFillSymbol).Style = esriSimpleFillStyle.esriSFSHollow;
ISimpleLineSymbol outline = new SimpleLineSymbolClass();
outline.Width = 1;
int seed = RandomSeed; // 获取种子
Random colorRandom = new Random(seed);
outline.Color = new RgbColorClass() { RGB = colorRandom.Next(256) * 65536 + colorRandom.Next(256) * 256 + colorRandom.Next(256) } as IColor;
(result as ISimpleFillSymbol).Outline = outline;
simpleRender.Symbol = result;
(pFeaLayer as IGeoFeatureLayer).Renderer = simpleRender as IFeatureRenderer;
};
if (!string.IsNullOrEmpty(DisplayField))
{
IGeoFeatureLayer pGeoLayer = pFeaLayer as IGeoFeatureLayer;
pGeoLayer.DisplayField = DisplayField;
pGeoLayer.DisplayAnnotation = IsLable;
}
TargetMap.AddLayer(pFeaLayer);
}
不使用seed:
使用seed: