ArcGIS.LegendBitmapGenerator.Generate C# (CSharp) Метод

Generate() публичный Метод

public Generate ( Bitmap icon, string text ) : Bitmap
icon System.Drawing.Bitmap
text string
Результат System.Drawing.Bitmap
        public Bitmap Generate(Bitmap icon, string text)
        {
            float iconWidth = icon.Width * (_resolution / icon.HorizontalResolution);
            if (iconWidth > 100) {
                iconWidth = 100;
            }
            SizeF textSize = _graphics.MeasureString(text, _font, (int)(_width - iconWidth - (_margin * (_indentLevel + 2))));
            float height = Math.Max(icon.Height * (_resolution / icon.VerticalResolution), textSize.Height);
            Bitmap result = new Bitmap(_width, (int)height);
            result.SetResolution(_resolution, _resolution);
            Graphics g = Graphics.FromImage(result);
            g.Clear(System.Drawing.Color.White);
            if (_antialias) {
                g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
            }
            g.DrawImage(icon, _margin * (_indentLevel + 1), 0);
            RectangleF textArea = new RectangleF(iconWidth + (_margin * (_indentLevel + 1)),
                                                 0,
                                                 _width - iconWidth - (_margin * (_indentLevel + 2)),
                                                 height);
            g.DrawString(text, _font, _brush, textArea);
            g.Dispose();
            return result;
        }

Same methods

LegendBitmapGenerator::Generate ( string text ) : Bitmap

Usage Example

Пример #1
0
 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;
 }