欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ArcGIS二次开发基础教程(03):保存文档和导出地图

程序员文章站 2022-06-12 16:48:05
...

ArcGIS二次开发基础教程(03):保存文档和导出地图

保存文档

  1. 保存:
//这里的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();
}
  1. 另存为
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 resolution
  export.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 files
  export.FinishExporting();
  export.Cleanup();

  return true;
}

历届GIS应用技能大赛开发题答案点这里,尚在不定期更新中