Vishful thinking…

Dynamic legend generation through the ADF

Posted in Uncategorized by viswaug on March 16, 2008

I have uploaded my code snippet to generate legend images through the ADF here. The purpose for this grew out of being able to print multiple map resources on the map through the ADF. I had blogged about it before here. I have made some changes to the code snippet after input from some other people trying to do the same thing. The features of the code snippet are:

  • Generates images at the required DPI
  • Customizable legend title
  • Handles group layers
  • Handles symbol groups
  • Creates a legend image of the desired height and width. If the legend entries overflow the height specified, they will be omitted. If all legend entries are required in the images, it should only involve a small change in code to remove the check for overflow.

Here is an example of a legend generated by the snippet.



13 Responses

Subscribe to comments with RSS.

  1. iamlaksh1 said, on March 19, 2008 at 4:10 am

    Very Good work. I loved work. I have reposted in my blog, so that it will get noticed and useful to others. Hope no issues!

  2. […] Click here to read the code.  Author Vish  […]

  3. Jimmy said, on April 24, 2008 at 12:21 pm

    Has any converted the legend code to VB?

  4. Brian said, on April 25, 2008 at 10:50 pm

    In the for loop

    foreach (IGISFunctionality entry in item.Functionalities)
    if (entry is IMapFunctionality)
    mf = entry as IMapFunctionality;

    the item.Functionalities count is zero, therefore not assigning mf to an IMapFunctionality object, and the whole function returns null.

    Why would my map not have an IGISFunctionality entry? Do you know how I would fix this?


  5. viswaug said, on April 25, 2008 at 11:42 pm

    Hi Brian,

    Try initializing your MapResourceItem by calling MapResourceItem.InitializeResource().


  6. Gokce said, on May 29, 2008 at 8:45 pm


    I am new to c# and ArcGIS Server, and need to customize Web mapping application to do same thing you did here. I copied this snipplet, however couldn’t make it work. I tried to call it from page_load, but it gives me error on Map map, then I added “using ESRI.ArcGIS.Web.ADF.UI.WebControls”. This solves Map problem but I couldn’t figure out BitMap. What should I do? What should I include more?

    Thanks for all the help,


  7. Thanan said, on September 18, 2008 at 2:37 am


    Very good post. Whar needs to be changed if i need to display only the items in the current map extent in the legend. Can you alos share the implementation code for above during printing .


  8. steve said, on October 14, 2008 at 3:26 pm

    were you aware of the static method on the toc.renderlegendhtml and the method of the scalebar and northarrow called draw()?

    Could help out a bit…

  9. Daniel said, on November 10, 2008 at 3:39 pm


    I’m also interested in the VB version of the code ….

    Thank you…

  10. osman homek said, on February 6, 2009 at 2:06 pm

    if doesnt work, replace “trying ..” region with this codes

    if need change 0 index replace to your index

    #region “Trying to get the legend information”

    Dictionary<string, KeyValuePair> legendInfo = new Dictionary<string, KeyValuePair>();
    int layerCounter = 0;

    IMapResource itemMP = map.MapResourceManagerInstance.GetResource(0);
    IMapTocFunctionality mtoc = itemMP.CreateFunctionality(typeof(IMapTocFunctionality), Guid.NewGuid().ToString()) as IMapTocFunctionality;
    IMapFunctionality mf = Map1.GetFunctionality(0);

    TocDataFrame[] tocDataFrames = mtoc.GetMapContents(mf.Name, WebImageFormat.PNG24, true, false);
    foreach (TocDataFrame tocDataFrame in tocDataFrames)
    foreach (TocLayer tocLayer in tocDataFrame)
    if (tocLayer.Visible)
    List<KeyValuePair> legends = GetLegendImages(tocLayer);
    if (legends != null && legends.Count > 0)
    foreach (KeyValuePair entry in legends)
    legendInfo.Add(layerCounter.ToString(), new KeyValuePair(entry.Key, entry.Value));



  11. abhi said, on March 24, 2009 at 1:57 pm

    Does anyone know, how to print the transparent graphics layer? I sent the buffered features to SOE, which has transparencies. But they are printing non-transparent.

    Any workaround besides saving it as a shapefile and bring it back to SOE.



  12. Usman said, on January 6, 2011 at 3:32 pm

    Hi Vish, i need the code for generating a dynamic legend on the extent. like on a button click a window popsup showing only the legend of the visible layers in the extent. m using ArcGIS server web ADF with C#. would really appreciate your help on the same.

  13. ebrahim said, on October 12, 2011 at 10:54 am

    I am new in GIS programming. I was lookin for a code take legends out of a map control.
    Thank you very much for your code.It was a big help.
    but I have a question.I need to update the legend by zoom in/out. Because in each zoom level we have different data and different symbols. so we need to update the legend by zooming .
    You extrac the legend from toc of the map. but toc is not a dynamic entity proportion to the map .
    so for a dynamic legend we need a another source updated by the map.
    I would really appreciate your help / idead.
    Thanks again.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: