ArcGIS二次开发基础教程(03):保存文档和导出地图
保存文档
保存://这里的path为全局变量 在打开文件获添加数据时赋值原路径//判断打开文件是否为mxd文件 是则保存 不是则另存为if(System.IO.File.Exists(path.Remove(path.IndexOf('.')) + ".mxd")){//对于已打开的mxd文档保存在原路径//获取mapcontrol中的地图数据IMXDContexts context = axMapControl.Map as IMXDContexts;IMapDocument mapDocumnet = new MapDocumnetClass();mapDocument.Open(path);mapDocument.ReplaceContexts(context);mapDocument.Save();}else{SaveAs();}
另存为
void SaveAs(){//没有mxd文档的新建一个saveFileDialog.Title = "保存文件";saveFileDialog.Filter = "ArcMap Document(*.mxd)|*.mxd";if(saveFileDialog.ShowDialog()==DialogResult.OK){IMXDContexts context = axMapControl.Map as IMXDContexts;IMapDocument mapDocumnet = new MapDocumnetClass();mapDocument.New(path);mapDocument.ReplaceContexts(context);mapDocument.Save();}}
导出地图
//将mapcontrol的显示范围导出为图片saveFileDialog.Title = "导出图片";//还有其他图片格式同理saveFileDialog.Filter = "JPEG(*.jpg)|*.jpg|PNG(*.png)|*.png|PDF(*.pdf)|*.pdf|BMP(*.bmp)|*.bmp|TIFF(*.tif)|*.tif";if(saveFileDialog.ShowDialog()==DialogResult.OK){IExport export = new ExportAllClass();string path = saveFileDialog.FileName;string extension = System.IO.Path.GetExtenion(path).Replace(".","").ToLower();switch(extension){case "jpg":export = new ExportJPEGClass();break;case "png":export = new ExportPNGClass();break;case "pdf":export = new ExportPDFClass();break;case "bmp":export = new ExportBMPClass();break;case "tif":export = new ExportTIFFClass();break;default:MessageBox.Show("Error");return;}if(ExportImage(export,axMapConrol1.ActiveView,fileName)){MessageBox.Show("Succeed");}else{MessageBox.Show("Error");}}//这里是参考的帮助文档bool ExportImage(IExport export,IActiveView activeView,string fileName){export.ExportFileName = fileName;// Microsoft Windows default DPI resolutionexport.Resolution = 96;// mapcontrol的显示范围tagRECT exportRECT = activeView.ExportFrame;IEnvelope envelope = new EnvelopeClass();envelope.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom);export.PixelBounds = envelope;Int32 hDC = export.StartExporting();activeView.Output(hDC, (Int16)export.Resolution, ref exportRECT, null, null);// Finish writing the export file and cleanup any intermediate filesexport.FinishExporting();export.Cleanup();return true;}
历届GIS应用技能大赛开发题答案点这里,尚在不定期更新中