public static Bitmap GetLegendImage(string serviceUri, int width, int height, int resolution, Font font, bool antialias) { MapServerProxy proxy = new MapServerProxy(serviceUri); string mapName = proxy.GetDefaultMapName(); ImageType imageType = new ImageType(); imageType.ImageFormat = esriImageFormat.esriImageBMP; imageType.ImageReturnType = esriImageReturnType.esriImageReturnMimeData; MapServerLegendInfo[] legends = proxy.GetLegendInfo(mapName, null, null, imageType); List<Bitmap> legendEntries = new List<Bitmap>(); Bitmap result = null; if (height > 0) { result = new Bitmap(width, height); result.SetResolution(resolution, resolution); } if (legends != null && legends.Length > 0) { Bitmap scratchBitmap = result; if (scratchBitmap == null) { scratchBitmap = new Bitmap(width, 500); scratchBitmap.SetResolution(resolution, resolution); } LegendBitmapGenerator gen = new LegendBitmapGenerator(scratchBitmap); gen.Margin = 10; gen.IndentLevel = 0; gen.Font = font; gen.Brush = new SolidBrush(System.Drawing.Color.Black); gen.Antialias = antialias; foreach (MapServerLegendInfo legend in legends) { if (legend.Name.Length > 0) { legendEntries.Add(gen.Generate(legend.Name)); } gen.IndentLevel += 1; foreach (MapServerLegendGroup group in legend.LegendGroups) { if (group.Heading.Length > 0) { legendEntries.Add(gen.Generate(group.Heading)); } gen.IndentLevel += 1; foreach (MapServerLegendClass legendClass in group.LegendClasses) { legendEntries.Add(gen.Generate(new Bitmap(new System.IO.MemoryStream(legendClass.SymbolImage.ImageData)), legendClass.Label)); } gen.IndentLevel -= 1; } gen.IndentLevel -= 1; } gen.Dispose(); } int legendEntryPadding = 2; if (result == null) { height = legendEntryPadding; foreach (Bitmap entry in legendEntries) { height += legendEntryPadding + entry.Height; } result = new Bitmap(width, height); result.SetResolution(resolution, resolution); } int currentY = legendEntryPadding; Graphics graphics = Graphics.FromImage(result); graphics.Clear(System.Drawing.Color.White); foreach (Bitmap entry in legendEntries) { if ((currentY + entry.Height) <= height) { graphics.DrawImage(entry, 0, currentY); currentY += legendEntryPadding + entry.Height; } } graphics.Dispose(); //result.MakeTransparent(result.GetPixel(0, 0)); return result; }