zoukankan      html  css  js  c++  java
  • ArcGIS Engine开发之地图导出

    关于地图导出的方法有很多,但是核心技术就那么一点。下面是从项目实战中总结的一部分地图导出的方法:(以全域导出和区域导出为例)

    1.由于地图导出用到的函数和方法容易重复,增加了工作量故首先将其进行封装成类(ExportMap类):用到的主要接口为:IActiveView(活动视图接口)、IGeometry(几何接口)、IRgbColor(颜色接口)、IElement(要素接口)等。

    具体的封装代码如下:

      1   class ExportMap
      2     {
      3         #region 输出视图
      4         public static void ExportView(IActiveView view, IGeometry pGeo, int Outputresoultion, int Width, int Height, string ExpPath, bool bRegion)
      5         {
      6             IExport pExport = null;
      7             tagRECT exportrect = new tagRECT();
      8             IEnvelope pEnvelop = pGeo.Envelope;
      9             string sType = System.IO.Path.GetExtension(ExpPath);
     10             switch (sType)
     11             {
     12                 case ".jpg":
     13                     pExport = new ExportJPEGClass();
     14                     break;
     15                 case ".bmp":
     16                     pExport = new ExportBMPClass();
     17                     break;
     18                 case ".gif":
     19                     pExport = new ExportGIFClass();
     20                     break;
     21                 case ".tif":
     22                     pExport = new ExportTIFFClass();
     23                     break;
     24                 case ".png":
     25                     pExport = new ExportPNGClass();
     26                     break;
     27                 case ".pdf":
     28                     pExport = new ExportPDFClass();
     29                     break;
     30                 default:
     31                     MessageBox.Show("没有输出格式,默认JPEG");
     32                     pExport = new ExportJPEGClass();
     33                     break;
     34             }
     35             pExport.ExportFileName = ExpPath;
     36             exportrect.left = 0;
     37             exportrect.top = 0;
     38             exportrect.right = Width;
     39             exportrect.bottom = Height;
     40             if (bRegion)
     41             {
     42                 view.GraphicsContainer.DeleteAllElements();
     43                 view.Refresh();
     44             }
     45             IEnvelope envelop = new EnvelopeClass();
     46             envelop.PutCoords((double)exportrect.left, (double)exportrect.top, (double)exportrect.right, (double)exportrect.bottom);
     47             pExport.PixelBounds = envelop;
     48             view.Output(pExport.StartExporting(), Outputresoultion, ref exportrect, pEnvelop, null);
     49             pExport.FinishExporting();
     50             pExport.Cleanup();
     51         }
     52         #endregion
     53         #region 获取RGB颜色
     54         private static  IRgbColor GetRgbColor(int intR,int intG,int intB)
     55         {
     56             IRgbColor pRgbColor=null;
     57             if(intR<0||intR>255||intG<0||intG>255||intB<0||intB>255)
     58             {
     59                 return pRgbColor;
     60             }
     61             pRgbColor=new RgbColorClass();
     62             pRgbColor.Red=intR;
     63             pRgbColor.Green=intG;
     64             pRgbColor.Blue=intB;
     65             return pRgbColor;
     66         }
     67         #endregion
     68         #region 创建图形元素
     69         /// <summary>
     70         /// 
     71         /// </summary>
     72         /// <param name="pGeomentry">几何图形</param>
     73         /// <param name="lineColor">边框颜色</param>
     74         /// <param name="fillColor">填充颜色</param>
     75         /// <returns></returns>
     76         public static IElement CreateElement(IGeometry pGeomentry, IRgbColor lineColor, IRgbColor fillColor)
     77         {
     78             if (pGeomentry == null || lineColor == null || fillColor == null)
     79             {
     80                 return null;
     81             }
     82             IElement pElem = null;
     83             try
     84             {
     85                 if (pGeomentry is IEnvelope)
     86 
     87                     pElem = new RectangleElementClass();
     88                 else if (pGeomentry is IPolygon)
     89                     pElem = new PolygonElementClass();
     90                 else if (pGeomentry is ICircularArc)
     91                 {
     92                     ISegment pSegCircle = pGeomentry as ISegment;
     93                     ISegmentCollection pSegColl = new PolygonClass();
     94                     object o = Type.Missing;
     95                     pSegColl.AddSegment(pSegCircle, ref o, ref o);
     96                     IPolygon pPolygon = pSegCircle as IPolygon;
     97                     pGeomentry = pPolygon as IGeometry;
     98                     pElem = new CircleElementClass();
     99                 }
    100                 else if (pGeomentry is IPolyline)
    101                     pElem = new LineElementClass();
    102                 if (pElem == null)
    103                     return null;
    104                 pElem.Geometry = pGeomentry;
    105                 IFillShapeElement pFElem = pElem as IFillShapeElement;
    106                 ISimpleFillSymbol pSymbol = new SimpleFillSymbolClass();
    107                 pSymbol.Color=fillColor ;
    108                 pSymbol.Outline.Color=lineColor;
    109                 pSymbol.Style = esriSimpleFillStyle.esriSFSCross;//图形元素的样式
    110                 if (pSymbol == null)
    111                 {
    112                     return null;
    113                 }
    114                 pFElem.Symbol = pSymbol;
    115             }
    116             catch(Exception ex)
    117             {
    118                 MessageBox.Show(ex.Message );
    119             }
    120             return pElem;
    121         }
    122         #endregion
    123         #region 视图窗口绘制几何图形元素
    124         /// <summary>
    125         /// 
    126         /// </summary>
    127         /// <param name="pGeometry">几何图形</param>
    128         /// <param name="activeView">活动视图</param>
    129         public static void AddElement(IGeometry pGeometry,IActiveView activeView)
    130         {
    131             IRgbColor fillColor=GetRgbColor(204,175,235);
    132             IRgbColor lineColor=GetRgbColor(0,0,0);
    133             IElement pEle=CreateElement(pGeometry,lineColor,fillColor );//调用图形元素的函数
    134             IGraphicsContainer pGC = activeView.GraphicsContainer;
    135             if (pGC != null)
    136             {
    137                 pGC.AddElement(pEle, 0);
    138                 activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, pEle, null);
    139             }
    140         }
    141         #endregion
    142         #region 全域导出
    143         /// <summary>
    144         /// 全域导出
    145         /// </summary>
    146         /// <param name="OutputResolution">输出分辨率</param>
    147         /// <param name="ExpPath">输出路径</param>
    148         /// <param name="view">视图</param>
    149         public static void ExportActiveView(int OutputResolution, string ExpPath, IActiveView view)
    150         {
    151             IExport pExport = null;
    152             tagRECT exportRect;
    153             IEnvelope envelop2 = view.Extent;
    154             int num = (int)Math.Round(view.ScreenDisplay.DisplayTransformation.Resolution);
    155             string sType = System.IO.Path.GetExtension(ExpPath);
    156             switch (sType)
    157             {
    158                 case ".jgp":
    159                     pExport = new ExportJPEGClass();
    160                     break;
    161                 case ".bmp":
    162                     pExport = new ExportBMPClass();
    163                     break;
    164                 case ".gif":
    165                     pExport = new ExportGIFClass();
    166                     break;
    167                 case ".tif":
    168                     pExport = new ExportTIFFClass();
    169                     break;
    170                 case ".png":
    171                     pExport = new ExportPNGClass();
    172                     break;
    173                 case ".pdf":
    174                     pExport = new ExportPDFClass();
    175                     break;
    176                 default: MessageBox.Show("No Export Foemat,the default format is JPEG!");
    177                     pExport = new ExportJPEGClass();
    178                     break;
    179             }
    180             pExport.ExportFileName = ExpPath;
    181             exportRect.left = 0; exportRect.top = 0;
    182             exportRect.right = (int)Math.Round((double)(view.ExportFrame.right * (((double)OutputResolution) / ((double)num))));
    183             exportRect.bottom = (int)Math.Round((double)(view.ExportFrame.bottom * (((double)OutputResolution) / ((double)num))));
    184             IEnvelope envelop = new EnvelopeClass();
    185             envelop.PutCoords((double)exportRect.left, (double)exportRect.top, (double)exportRect.right, (double)exportRect.bottom);
    186             pExport.PixelBounds = envelop;
    187             view.Output(pExport.StartExporting(), OutputResolution, ref exportRect, envelop2, null);
    188             pExport.FinishExporting();
    189             pExport.Cleanup();
    190         }
    191         #endregion
    192         #region  区域导出
    193         /// <summary>
    194         /// 区域导出
    195         /// </summary>
    196         /// <param name="pGeo">输出的图形</param>
    197         /// <param name="OutputResolution">输出的范围</param>
    198         /// <param name="ExpPath">输出路径</param>
    199         /// <param name="view">视图</param>
    200         public static void ExportRegion(IGeometry pGeo, int OutputResolution, string ExpPath, IActiveView view)
    201         {
    202             IExport export = null;
    203             IWorldFileSettings setting = null;
    204             IEnvelope envelope2 = pGeo.Envelope;
    205             string str = ExpPath.Substring(ExpPath.Length - 3, 3).ToUpper();
    206             switch (str)
    207             {
    208                 case "JPG":
    209                     setting = new ExportJPEGClass();
    210                     export = new ExportJPEGClass();
    211                     setting = export as IWorldFileSettings;
    212                     setting.MapExtent = envelope2;
    213                     setting.OutputWorldFile = false;
    214                     break;
    215                 case "BMP":
    216                     setting = new ExportBMPClass();
    217                     export = new ExportBMPClass();
    218                     setting = export as IWorldFileSettings;
    219                     setting.MapExtent = envelope2;
    220                     setting.OutputWorldFile = false;
    221                     break;
    222                 case "TIF":
    223                     setting = new ExportTIFFClass();
    224                     export = new ExportTIFFClass();
    225                     setting = export as IWorldFileSettings;
    226                     setting.MapExtent = envelope2;
    227                     setting.OutputWorldFile = false;
    228                     break;
    229                 case "PNG":
    230                     setting = new ExportPNGClass();
    231                     export = new ExportPNGClass();
    232                     setting = export as IWorldFileSettings;
    233                     setting.MapExtent = envelope2;
    234                     setting.OutputWorldFile = false;
    235                     break;
    236                 default: break;
    237             }
    238             if (setting == null)
    239             {
    240                 export.ExportFileName = ExpPath;
    241                 int num = (int)Math.Round(view.ScreenDisplay.DisplayTransformation.Resolution);
    242                 tagRECT grect2 = new tagRECT();//实例化矩形
    243                 IEnvelope envelop3 = new EnvelopeClass();
    244                 view.ScreenDisplay.DisplayTransformation.TransformRect(envelope2, ref grect2, 9);
    245                 grect2.left = 0;
    246                 grect2.top = 0;
    247                 grect2.right = (int)Math.Round((double)(grect2.right - grect2.left) * (((double)OutputResolution) / ((double)num)));
    248                 grect2.bottom = (int)Math.Round((double)((grect2.bottom - grect2.top) * (((double)OutputResolution) / ((double)num))));
    249                 envelop3.PutCoords((double)grect2.left, (double)grect2.top, (double)grect2.right, (double)grect2.bottom);
    250                 export.PixelBounds = envelop3;
    251                 view.GraphicsContainer.DeleteAllElements();
    252                 view.Output(export.StartExporting(), OutputResolution, ref grect2, envelope2, null);
    253                 export.FinishExporting();
    254                 export.Cleanup();
    255                 AddElement(pGeo, view);
    256             }
    257 
    258         }
    259         #endregion
    260     }

     2.添加输出设置窗体,分别有,输出图片的高、宽、分辨率、输出保存路径、导出按钮。

    具体的设置代码如下:

      1 public partial class ExportMapForm : DevExpress.XtraEditors.XtraForm
      2     {
      3         //定义全局变量
      4         private string pSavePath = "";
      5         private IActiveView pActiveView;
      6         private IGeometry pGeometry = null;
      7         #region 只读属性,地图导出空间图形
      8         public IGeometry GetGeometry
      9         {
     10             set { pGeometry = value; }
     11         }
     12         private bool bRegion = true;
     13         #endregion
     14         /// <summary>
     15         /// 只读属性,是全域导出还是区域导出
     16         /// </summary>
     17         public bool IsRegion
     18         {
     19             set { bRegion = value; }
     20         }
     21 
     22         //获取主窗口的MapControl控件
     23         public ExportMapForm(AxMapControl mainAxMapControl)
     24         {
     25             InitializeComponent();
     26 
     27             pActiveView = mainAxMapControl.ActiveView;
     28             
     29         }
     30 
     31         private void ExportMapForm_Load(object sender, EventArgs e)
     32         {
     33 
     34         }
     35         #region 输入窗口的大小
     36         private void InitFormSize()
     37         {
     38             cboResoultion.Text = pActiveView.ScreenDisplay.DisplayTransformation.Resolution.ToString();
     39             cboResoultion.Items.Add(cboResoultion.Text);
     40             if (bRegion)
     41             {
     42                 IEnvelope pEnvelop = pGeometry.Envelope;
     43                 tagRECT pRECT = new tagRECT();
     44                 pActiveView.ScreenDisplay.DisplayTransformation.TransformRect(pEnvelop, ref pRECT,15);
     45                 if (cboResoultion.Text != "")
     46                 {
     47                     txtWidth.Text = pRECT.right.ToString();
     48                     txtHeight.Text = pRECT.bottom.ToString();
     49                 }
     50             }
     51             else
     52             {
     53                 if (cboResoultion.Text != "")
     54                 {
     55                     txtWidth.Text = pActiveView.ExportFrame.right.ToString();
     56                     txtHeight.Text = pActiveView.ExportFrame.bottom.ToString();
     57                 }
     58             }
     59         }
     60         #endregion
     61         #region combox的ChangeIndex事件
     62         private void cboResoultion_SelectedIndexChanged(object sender, EventArgs e)
     63         {
     64             double num = (int)Math.Round(pActiveView.ScreenDisplay.DisplayTransformation.Resolution);
     65             if (cboResoultion.Text == "")
     66             {
     67                 txtWidth.Text = "";
     68                 txtHeight.Text = "";
     69                 return;
     70             }
     71             if (bRegion)
     72             {
     73                 IEnvelope pEnvelop = pGeometry.Envelope;
     74                 tagRECT pRECT = new tagRECT();
     75                 pActiveView.ScreenDisplay.DisplayTransformation.TransformRect(pEnvelop, ref pRECT,15);
     76                 if (cboResoultion.Text != "")
     77                 {
     78                     txtWidth.Text = Math.Round((double)(pRECT.right * (double.Parse(cboResoultion.Text) / (double)num))).ToString();
     79                 }
     80             }
     81             else
     82             {
     83                 txtWidth.Text = Math.Round((double)(pActiveView.ExportFrame.right * (double.Parse(cboResoultion.Text) / (double)num))).ToString();
     84                 txtHeight.Text = Math.Round((double)(pActiveView.ExportFrame.bottom * (double.Parse(cboResoultion.Text) / (double)num))).ToString();
     85             }
     86         }
     87         #endregion
     88         #region 保存按钮的单击事件
     89         private void btnExPath_Click(object sender, EventArgs e)
     90         {
     91             SaveFileDialog sfdExportMap = new SaveFileDialog();
     92             sfdExportMap.DefaultExt = "jpg|bmp|gif|tif|png|pdf";
     93             sfdExportMap.Filter = "JPGE 文件(*.jpg)|*.jpg|BMP 文件(*.bmp)|*.bmp|GIF 文件(*.gif)|*.gif|TIF 文件(*.tif)|*.tif|PNG 文件(*.png)|*.png|PDF 文件(*.pdf)|*.pdf";
     94             sfdExportMap.OverwritePrompt = true;//重复写入时提示错误
     95             sfdExportMap.Title = "保存为";
     96             txtExPath.Text = "";
     97             if (sfdExportMap.ShowDialog() != DialogResult.Cancel)
     98             {
     99                 pSavePath = sfdExportMap.FileName;
    100                 txtExPath.Text = sfdExportMap.FileName;
    101             }
    102         }
    103         #endregion
    104         #region 导出按钮单击事件
    105         private void btnExPort_Click(object sender, EventArgs e)
    106         {
    107             if (txtExPath.Text == "")
    108             {
    109                 MessageBox.Show("请先确定导出路径!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    110                 return;
    111             }
    112             else if (cboResoultion.Text == "")
    113             {
    114                 if (txtExPath.Text == "")
    115                 {
    116                     MessageBox.Show("请输入分辨率!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    117                     return;
    118                 }
    119             }
    120             else if (Convert.ToInt16(cboResoultion.Text) == 0)
    121             {
    122                 MessageBox.Show("请正确输入分辨率!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    123                 return;
    124             }
    125             else
    126             {
    127                 try
    128                 {
    129                     int resoultion = int.Parse(cboResoultion.Text);//输出图片的分辨率
    130                     int width = int.Parse(cboResoultion.Text);//输出图片的宽度
    131                     int height = int.Parse(cboResoultion.Text);//输出图片的高度
    132                     ExportMap.ExportView(pActiveView, pGeometry, resoultion, width, height, pSavePath, bRegion);
    133                     pActiveView.GraphicsContainer.DeleteAllElements();
    134                     pActiveView.Refresh();
    135                     MessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    136                 }
    137                 catch (Exception)
    138                 {
    139                     MessageBox.Show("导出失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    140                 }
    141             }
    142         }
    143         #endregion
    144         #region 取消按钮的单击事件
    145         private void btnCancel_Click(object sender, EventArgs e)
    146         {
    147             //局部导出时没有导出图像就退出
    148             pActiveView.GraphicsContainer.DeleteAllElements();
    149             pActiveView.Refresh();
    150             Dispose();
    151         }
    152         #endregion
    153         #region 图片导出窗口Close事件
    154         private void ExportMapForm_FormClosed(object sender, FormClosedEventArgs e)
    155         {
    156             //局部导出时没有导出图像就关闭
    157             pActiveView.GraphicsContainer.DeleteAllElements();
    158             pActiveView.Refresh();
    159             Dispose();
    160 
    161         }
    162         #endregion
    View Code

    3.在main窗体中进行实例化输出窗体:private ExportMapForm frmExpMap = null;

    4.在MainMap Control_OnMouseDown中进行写入case:(只适合区域导出,单击选择边框)

     1 #region 地图的区域导出
     2                     case "ExportRegion":
     3                         //删除视图中的数据
     4                         mainMapControl.ActiveView.GraphicsContainer.DeleteAllElements();
     5                         mainMapControl.ActiveView.Refresh();
     6                         IPolygon pPolygon = DrawPolygon(mainMapControl);
     7                         if (pPolygon == null) return;
     8                         ExportMap.AddElement(pPolygon, mainMapControl.ActiveView);
     9                         if (frmExpMap == null || frmExpMap.IsDisposed)
    10                         {
    11                             frmExpMap = new ExportMapForm(mainMapControl);
    12                         }
    13                         frmExpMap.IsRegion = true;
    14                         frmExpMap.GetGeometry = pPolygon as IGeometry;
    15                         frmExpMap.Show();
    16                         frmExpMap.Activate();
    17                         break;
    18                     #endregion

    5.全域导出和区域导出按钮的单击事件代码:

     1       #region 地图导出之全域导出
     2         private void btnExportMap_ItemClick(object sender, ItemClickEventArgs e)
     3         {
     4             if (frmExpMap == null || frmExpMap.IsDisposed)
     5             {
     6                 frmExpMap = new ExportMapForm(mainMapControl);
     7             }
     8             frmExpMap.IsRegion = false;
     9             frmExpMap.GetGeometry = mainMapControl.ActiveView.Extent;
    10             frmExpMap.Show();
    11             frmExpMap.Activate();
    12         }
    13         #endregion
    14         #region 地图导出之区域导出
    15         private void btnExportRegionMap_ItemClick(object sender, ItemClickEventArgs e)
    16         {
    17             mainMapControl.CurrentTool = null;
    18             mainMapControl.MousePointer = esriControlsMousePointer.esriPointerCrosshair;
    19             pMouseOperate = "ExportRegion";
    20         }
    21         #endregion
  • 相关阅读:
    Android中AsyncTask与handler
    AndroidTimer使用(三)补充篇
    Android的消息机制(一)
    Android消息处理机制(二)
    java操作Excel文件(二)
    java操作excel
    Android APK反编译详解(附图)
    Android中Handler的使用方法——在子线程中更新界面
    Android线程间通信的Message机制
    AndroidTimer使用(二)
  • 原文地址:https://www.cnblogs.com/dongteng/p/5906614.html
Copyright © 2011-2022 走看看