Home
Manage Your Code
Snippet: Dynamic Legend Generation (C#)
Title: Dynamic Legend Generation Language: C#
Description: This snippet of code should allow you generate a legend image with the desired specification given the Map Control Views: 3100
Author: Vish Uma Date Added: 3/10/2008
Copy Code  
1public Bitmap GetLegendImage(Map map, int width, int height, int printResolution, string legendTitle)
2        {
3            Bitmap legendImage = null;
4            #region "Trying to get the legend information"   ...            #endregion
48            #region "Create the legend dynamically"   ...            #endregion
122
123
124            return legendImage;
125        }
126
127        private List<KeyValuePair<string, CartoImage>> GetLegendImages(TocLayer tocLayer)
128        {
129            List<KeyValuePair<string, CartoImage>> legends = new List<KeyValuePair<string, CartoImage>>();
130            if (tocLayer != null && tocLayer.Visible)
131            {
132                if (tocLayer.TocSymbolGroupCount > 0)
133                {
134                    if (tocLayer.TocSymbolGroupCount == 1)
135                    {
136                        TocSymbolGroup symbolGroupSingle = tocLayer.GetTocSymbolGroup(0);
137
138                        //Looks like there is no way to differentiate between a layer with a simple single symbol
139                        //and a layer with a group symbol with only one entry
140
141                        //So, if we encounter a group symbol with only one entry, assume it is a simple single legend
142                        //This actually does not make a difference for people reading the map as both should mean the same thing
143                        //The ESRI ADF TOC control works the same way
144                        if (symbolGroupSingle.Count == 1)
145                        {
146                            TocSymbol tocSymbol = symbolGroupSingle[0];
147                            legends.Add(new KeyValuePair<string, CartoImage>(tocLayer.LayerName, tocSymbol.Image));
148                        }
149                        else
150                        {
151                            System.Collections.IEnumerator e = tocLayer.GetTocSymbolGroups();
152                            //add the layer name
153                            if(e.MoveNext() && e.Current != null)
154                                legends.Add(new KeyValuePair<string, CartoImage>(tocLayer.LayerName, null));
155
156                            e.Reset();
157                            while (e.MoveNext())
158                            {
159                                //Add the group name
160                                TocSymbolGroup symbolGroup = e.Current as TocSymbolGroup;
161                                legends.Add(new KeyValuePair<string, CartoImage>(symbolGroup.Heading, null));
162
163                                foreach (TocSymbol tocSymbol in symbolGroup)
164                                {
165                                    //add group entries
166                                    legends.Add(new KeyValuePair<string, CartoImage>(tocSymbol.Label, tocSymbol.Image));
167                                }
168                            }
169                        }
170                    }
171                    else
172                    {
173                        System.Collections.IEnumerator e = tocLayer.GetTocSymbolGroups();
174                        if (e.MoveNext() && e.Current != null)
175                            legends.Add(new KeyValuePair<string, CartoImage>(tocLayer.LayerName, null));
176                        e.Reset();
177                        while (e.MoveNext())
178                        {
179                            TocSymbolGroup symbolGroup = e.Current as TocSymbolGroup;
180                            legends.Add(new KeyValuePair<string, CartoImage>(symbolGroup.Heading, null));
181
182                            foreach (TocSymbol tocSymbol in symbolGroup)
183                            {
184                                legends.Add(new KeyValuePair<string, CartoImage>(tocSymbol.Label, tocSymbol.Image));
185                            }
186                        }
187                    }
188                }
189
190                if (tocLayer.TocLayerCount > 0)
191                {
192                    System.Collections.IEnumerator subLayers = tocLayer.GetTocLayers();
193                    while (subLayers.MoveNext())
194                    {
195                        List<KeyValuePair<string, CartoImage>> subLayerLegends = GetLegendImages(subLayers.Current as TocLayer);
196                        if (subLayerLegends != null)
197                            legends.AddRange(subLayerLegends);
198                    }
199                }
200            }
201            return legends;
202        }
Notes
Please let me know if you have any problems, questions or suggestions for the snippet above.