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: