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?