Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I've been building legends for my javascript websites that use 9.3.1 services. I know there's the ESRI legend tool for older services but I don't feel that it does a good job of producing legend images for raster layers, especially continuous raster layers. It also drops things like the symbol headings. Ideally I'd like to be able to automatically mimick the look of the ArcMap TOC.

I've been trying to build some ArcObjects code to create a Legend to attach to the Page Layout and then export that to an image, but I can't seem to be able to find the code necessary to export the image after I build the legend.

the following code is the basic crux of what I've tried so far:

    void OpenMXDViaMapDocument(string path)
    {

        IMapDocument pMapDocument = new MapDocumentClass();            

        if (pMapDocument.get_IsMapDocument(path))
        {
            pMapDocument.Open(path, null);
            IMap pMap;

            IPageLayout layout = pMapDocument.PageLayout;


            for (int i = 0; i <= pMapDocument.MapCount - 1; i++)
            {
                pMap = pMapDocument.get_Map(i);
                UID pId = new UID();
                pId.Value ="esriCarto.Legend";
                IMapSurround surround = pMap.CreateMapSurround(pId, null);
                ILegend iLegend = (ILegend)surround;
                int p = iLegend.ItemCount;

                iLegend.AutoVisibility = false;

                System.Drawing.Bitmap myBitmap = new Bitmap(500,500);
                IntPtr ptr = myBitmap.GetHbitmap();

                ESRI.ArcGIS.Display.SimpleDisplayClass sdc = new ESRI.ArcGIS.Display.SimpleDisplayClass();

                for (int y = 0; y < p; y++)
                {
                    ILegendItem iLegendItem = iLegend.get_Item(y);
                    LegendFormat legendFormat = new LegendFormat();
                    legendFormat.ShowTitle = true;

                    iLegendItem.CreateGraphics(sdc,legendFormat);
                    IEnumElement enumEles = iLegendItem.Graphics;
                    enumEles.Reset();
                    IGraphicElement ige = (IGraphicElement)enumEles.Next();
                    while (ige != null)
                    {
                        FileStreamClass fs_class = new FileStreamClass();
                        richTextBox1.Text = richTextBox1.Text + "#"+y+" - Graphic Element Type " + identifyGraphicElementType(ige) +"\n";
                        if (identifyGraphicElementType(ige).Equals("BmpPictureElement"))
                        {
            //All of them are apparently BmpPictureElements
                            BmpPictureElement bmpE;
                            //Java version has bmpE.save(IStream) ? 
                            fs_class.SaveToFile("C:\\utils\\newImages\\{0}_{1}.bmp");
                        }
                        ige = (IGraphicElement)enumEles.Next();
                    }                        
                }

            }

        }
    }

I would also like to generate an image for each map layer showing the title, symbol heading if any and the symbol. How can I do this?

share|improve this question
add comment

1 Answer

Maybe try this:

Create a layout with the mapframe located outside of the page extent (since you're not interested in drawing it). Make the size of the layout only a tiny bit larger than the legend frame.

For each raster layer, load the legend item into the legend. Should be just one legend item for the types of layers you are doing, right?

It's been a while, but somewhere in there, you might need to call IActiveView.Activate to get the legend to refresh.

Export the entire layout using code from the Export Active View sample.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.