ArcGIS Engine开发之地图导出

Posted 东腾的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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  

以上是关于ArcGIS Engine开发之地图导出的主要内容,如果未能解决你的问题,请参考以下文章

ArcGIS Engine开发之地图基本操作

ArcGIS Engine开发之地图基本操作

ArcGIS Engine开发之地图基本操作

ArcGIS Engine开发之地图浏览

ArcGIS Engine开发之视图同步

ArcGIS Engine开发之空间查询