使用C#绘制MO中地图的图例

    技术2022-05-11  65

    using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;

    namespace Gissky.Geography.Legend{/// <summary>/// Legend 的摘要说明。//code by aji 2004-10-01/// </summary>public class LegendSingle : System.Windows.Forms.UserControl{  private ESRI.MapObjects2.Core.AxMap m_MapObj;  System.Drawing.Bitmap bmp ;  System.Drawing.Graphics g ;  int iTextDrawX;  private System.Windows.Forms.VScrollBar ScrollBarv;  private System.Windows.Forms.HScrollBar ScrollBarh;  private System.Windows.Forms.PictureBox LegendPicture;  private int bmpHeight;  /// <summary>  /// 必需的设计器变量。  /// </summary>  private System.ComponentModel.Container components = null;  public LegendSingle()  {   //   // Windows 窗体设计器支持所必需的   //   InitializeComponent();

       //   // TOD 在 InitializeComponent 调用后添加任何构造函数代码   //  }

      public LegendSingle(ESRI.MapObjects2.Core.AxMap obj)  {   //   // Windows 窗体设计器支持所必需的   //   InitializeComponent();

       //   // TOD 在 InitializeComponent 调用后添加任何构造函数代码   //   this.m_MapObj = obj;     }

      /// <summary>  /// 清理所有正在使用的资源。  /// </summary>  protected override void Dispose( bool disposing )  {   if( disposing )   {    if(components != null)    {     components.Dispose();    }   }   base.Dispose( disposing );  }

      #region Windows 窗体设计器生成的代码  /// <summary>  /// 设计器支持所需的方法 - 不要使用代码编辑器修改  /// 此方法的内容。  /// </summary>  private void InitializeComponent()  {   this.LegendPicture = new System.Windows.Forms.PictureBox();   this.ScrollBarv = new System.Windows.Forms.VScrollBar();   this.ScrollBarh = new System.Windows.Forms.HScrollBar();   this.SuspendLayout();   //    // LegendPicture   //    this.LegendPicture.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)     | System.Windows.Forms.AnchorStyles.Right)));   this.LegendPicture.BackColor = System.Drawing.SystemColors.HighlightText;   this.LegendPicture.Location = new System.Drawing.Point(0, 0);   this.LegendPicture.Name = "LegendPicture";   this.LegendPicture.Size = new System.Drawing.Size(200, 300);   this.LegendPicture.TabIndex = 3;   this.LegendPicture.TabStop = false;   //    // ScrollBarv   //    this.ScrollBarv.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)     | System.Windows.Forms.AnchorStyles.Right)));   this.ScrollBarv.Location = new System.Drawing.Point(184, 0);   this.ScrollBarv.Name = "ScrollBarv";   this.ScrollBarv.Size = new System.Drawing.Size(16, 300);   this.ScrollBarv.TabIndex = 1;   this.ScrollBarv.Scroll += new System.Windows.Forms.ScrollEventHandler(this.ScrollBarv_Scroll);   //    // ScrollBarh   //    this.ScrollBarh.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)     | System.Windows.Forms.AnchorStyles.Right)));   this.ScrollBarh.Location = new System.Drawing.Point(0, 284);   this.ScrollBarh.Name = "ScrollBarh";   this.ScrollBarh.Size = new System.Drawing.Size(200, 16);   this.ScrollBarh.TabIndex = 2;   this.ScrollBarh.Visible = false;   //    // LegendSingle   //    this.BackColor = System.Drawing.SystemColors.ControlLightLight;   this.Controls.Add(this.ScrollBarh);   this.Controls.Add(this.ScrollBarv);   this.Controls.Add(this.LegendPicture);   this.Name = "LegendSingle";   this.Size = new System.Drawing.Size(200, 300);   this.Resize += new System.EventHandler(this.LegendSingle_Resize);   this.ResumeLayout(false);

      }  #endregion  public ESRI.MapObjects2.Core.AxMap AxMap  {   get   {    return this.m_MapObj;   }   set   {    this.m_MapObj = value;   }  }  public void InitLegend()  {   this.iTextDrawX = 50; //文字绘制的位置   bmpHeight = 20 + 28 * this.m_MapObj.Layers.Count;  //根据层的数目图片的高度   bmp= new Bitmap(500,bmpHeight);   if(this.bmpHeight > this.LegendPicture.Height)     this.LegendPicture.Size = new Size(this.LegendPicture.Size.Width,bmpHeight); //分配picturebox的大小   this.ScrollBarv.Maximum = bmpHeight - this.ScrollBarv.Height;  //滚动条的最大值   if(this.LegendPicture.Height > this.ScrollBarv.Height ) //是否显示滚动条    this.ScrollBarv.Visible = true;   else    this.ScrollBarv.Visible = false;   g= Graphics.FromImage(bmp);   g.Clear(Color.White);   DrawPicture();  }  /// <summary>  /// 根据mapobject空间中的地图分别绘制符号  /// </summary>  private void DrawPicture()  {      int iCount = this.m_MapObj.Layers.Count;   for(int i=0 ;i<iCount ;i++)   {    ESRI.MapObjects2.Core.MapLayer lyr = this.m_MapObj.Layers.Item(i) as ESRI.MapObjects2.Core.MapLayer;    if(lyr != null)    {    tch(lyr.shapeType)     {      case ESRI.MapObjects2.Core.ShapeTypeConstants.moShapeTypePoint:      case ESRI.MapObjects2.Core.ShapeTypeConstants.moShapeTypeMultipoint:       DrawPoint(lyr,10,10 + 28 * i);       break;      case ESRI.MapObjects2.Core.ShapeTypeConstants.moShapeTypeLine:       DrawLines(lyr,10,10 + 28 * i);       break;      case ESRI.MapObjects2.Core.ShapeTypeConstants.moShapeTypePolygon:      case ESRI.MapObjects2.Core.ShapeTypeConstants.moShapeTypeEllipse:      case ESRI.MapObjects2.Core.ShapeTypeConstants.moShapeTypeRectangle:       DrawRectangle(lyr,10,10 + 28 * i);       break;     }    }     else    {     //影像     ;    }   }   this.LegendPicture.Image = this.bmp;  }

      /// <summary>  /// 绘制点符号的图例  /// </summary>  /// <param name="lyr">图层对象</param>  /// <param name="x">绘制的坐标X</param>  /// <param name="y">绘制的坐标Y</param>  private void DrawPoint(ESRI.MapObjects2.Core.MapLayer lyr,float x,float y)  {   //定义画笔画刷 0、1、2样式的边框如果有的话都为1 ,3样式无边框效果   x = x + 6;   y = y + 4;   System.Drawing.Pen p;   System.Drawing.SolidBrush FillBrush;   System.Drawing.Color fillColor = System.Drawing.ColorTranslator.FromWin32(System.Convert.ToInt32(lyr.Symbol.Color));   System.Drawing.Color outlineColor = System.Drawing.ColorTranslator.FromWin32(System.Convert.ToInt32(lyr.Symbol.OutlineColor));   int iSize =12;      float XYadd = 4  ; //绘制 20 X 20 的大小   //初始化 填充画刷   FillBrush = new SolidBrush(fillColor);   if(lyr.Symbol.Style == 4)   {//字符集 符号需要设置相应的字体    g.DrawString(System.Convert.ToChar(lyr.Symbol.CharacterIndex).ToString(),new Font(lyr.Symbol.Font.Name,iSize),FillBrush,x ,y);       }   else   {    //初始化 画笔    if(lyr.Symbol.Outline)     p= new Pen(outlineColor,1);    else     p = new Pen(fillColor,0); //粗为0            //根据样式绘制符号        if(lyr.Symbol.Style == 0)    {//符号 “圆”     g.DrawEllipse(p,x + XYadd,y,iSize,iSize);     if(lyr.Symbol.Outline)      g.FillEllipse(FillBrush,x + XYadd + 1 ,y  + 1 ,iSize - 2,iSize -2); //有边框     else      g.FillEllipse(FillBrush,x + XYadd ,y ,iSize,iSize); //无边框                            }       else if(lyr.Symbol.Style==1)    {//符号 “正方形”     g.DrawRectangle(p,x + XYadd,y,iSize,iSize);     if(lyr.Symbol.Outline)      g.FillRectangle(FillBrush,x + XYadd + 1,y + 1,iSize -1 ,iSize-1);     else      g.FillRectangle(FillBrush,x + XYadd,y,iSize,iSize);    }    else if(lyr.Symbol.Style == 2)    {// 符号 “三角形”     iSize = 14 ;//三角形要大些     g.DrawPolygon(p,new PointF[] { new PointF(x + XYadd + iSize/2 ,y + 1),new PointF( x + XYadd + 1,y+iSize),new PointF( x + XYadd + iSize-1,y + iSize)});     if(lyr.Symbol.Outline)      g.FillPolygon(FillBrush,new PointF[] { new PointF(x + XYadd + iSize/2 ,y + 2),new PointF(x + XYadd + 2 ,y + iSize),new PointF(x + XYadd + iSize -2,y + iSize)});     else      g.FillPolygon(FillBrush,new PointF[] { new PointF(x + XYadd + iSize/2,y + 1),new PointF(x + XYadd + 1,y + iSize),new PointF( x + XYadd + iSize-1,y + iSize)});    }    else if(lyr.Symbol.Style == 3)    {     //符号“+”     System.Drawing.Pen lineP =  new Pen(fillColor,2);     g.DrawLine(lineP,new PointF(x + XYadd + iSize/2 ,y),new PointF(x + XYadd + iSize/2 ,y + iSize));     g.DrawLine(lineP,new PointF(x + XYadd,y + iSize/2),new PointF(x + XYadd + iSize,y + iSize/2));              }   }   g.DrawString (lyr.Name, new Font("宋体",10),System.Drawing.Brushes.Black,iTextDrawX,y +2 );   GC.Collect();  }  private void DrawLines(ESRI.MapObjects2.Core.MapLayer lyr,float x,float y)  {   //定义画笔画刷 0、1、2样式的边框如果有的话都为1 ,3样式无边框效果   x = x + 2;   y = y + 5;   System.Drawing.Pen p;   System.Drawing.Color fillColor = System.Drawing.ColorTranslator.FromWin32(System.Convert.ToInt32(lyr.Symbol.Color));   p= new Pen(fillColor,2);  tch(lyr.Symbol.Style)   {    case 0:      break;    case 1:     p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;     break;    case 2:     p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;     break;    case 3:     p.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;     break;    case 4:     p.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot;     break;    default:     break;

       }   g.DrawLine(p,new PointF(x,y+10),new PointF(x + 26,y+10));   g.DrawString (lyr.Name, new Font("宋体",10),System.Drawing.Brushes.Black,iTextDrawX,y + 2);   GC.Collect();  }  private void DrawRectangle(ESRI.MapObjects2.Core.MapLayer lyr,float x,float y)  {   x = x + 2;   y = y + 5;   System.Drawing.Pen p;   System.Drawing.SolidBrush solidBrush;   System.Drawing.Drawing2D.HatchBrush FillBrush = null;   System.Drawing.Color fillColor = System.Drawing.ColorTranslator.FromWin32(System.Convert.ToInt32(lyr.Symbol.Color));   System.Drawing.Color outlineColor = System.Drawing.ColorTranslator.FromWin32(System.Convert.ToInt32(lyr.Symbol.OutlineColor));   int iWidth =26;   int iHeight = 15;   //设置画笔的颜色和粗细   if(lyr.Symbol.Outline)     p= new Pen(outlineColor,1);    else     p = new Pen(fillColor,0); //粗为0    //绘制外边框矩形   g.DrawRectangle(p,x,y,iWidth,iHeight);     tch(lyr.Symbol.Style)   {        case 0:     solidBrush = new SolidBrush(fillColor);     g.FillRectangle(solidBrush,x+1,y+1,iWidth - 1,iHeight -1);     break;    case 1:          g.FillRectangle(System.Drawing.Brushes.White,x+1,y+1,iWidth - 1,iHeight -1);     break;    case 2:    case 3:    case 4:    case 5:    case 6:    case 7:    case 8:    case 9:    case 10:    tch(lyr.Symbol.Style)     {      case 2: //水平线填充       FillBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.NarrowHorizontal,fillColor,System.Drawing.Color.White);              break;      case 3: //竖线填充       FillBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Vertical,fillColor,System.Drawing.Color.White);        break;      case 4: //右斜45%       FillBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.ForwardDiagonal,fillColor,System.Drawing.Color.White);        break;      case 5: //左斜45%       FillBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.BackwardDiagonal,fillColor,System.Drawing.Color.White);        break;      case 6: //网格       FillBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.SmallGrid,fillColor,System.Drawing.Color.White);        break;      case 7: //对角十字叉       FillBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.OutlinedDiamond,fillColor,System.Drawing.Color.White);        break;      case 8: //        FillBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Percent70,fillColor,System.Drawing.Color.White);        break;      case 9: //9跟10差别不大,一下子.net里也找不到好发填充方式       FillBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Percent50,fillColor,System.Drawing.Color.White);        break;      case 10: //       FillBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Percent50,fillColor,System.Drawing.Color.White);        break;      default:       break;     }     g.FillRectangle(FillBrush,x+1,y+1,iWidth - 1,iHeight -1);     break;    default:     break;   }   //绘制图层名称   g.DrawString (lyr.Name, new Font("宋体",10),System.Drawing.Brushes.Black,iTextDrawX,y+2);   GC.Collect();  }

      private void ScrollBarv_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)  {//如果图例图片太高超出了空间的高度则显示滚动条   this.LegendPicture.Location = new Point(this.LegendPicture.Location.X,- e.NewValue);  }

      private void LegendSingle_Resize(object sender, System.EventArgs e)  { //控件调整大小的时候,重新计算并设置滚动条的状态   if(this.ScrollBarv.Height >= this.bmpHeight)   {    this.ScrollBarv.Visible = false;    this.LegendPicture.Location = new Point(this.LegendPicture.Location.X,0);   }   else   {    this.ScrollBarv.Visible = true;    this.ScrollBarv.Maximum = bmpHeight - this.ScrollBarv.Height;  //滚动条的最大值    this.ScrollBarv.Minimum = 0;    this.ScrollBarv.Value = 0;    this.LegendPicture.Location = new Point(this.LegendPicture.Location.X,- this.ScrollBarv.Value );   }  }}}

        

    中国GIS资讯网  (http://www.gissky.com/bbs/index.asp)

    ESRI(ArcGIS)  (http://www.gissky.com/bbs/list.asp?boardid=19) 


    最新回复(0)