ArcEngine实现将shp文件保存为MXD文件


MXD文件的保存分三种情况,第一种情况:将已有MXD文件修改后进行保存。第二种:将已有MXD文件另存为MXD文件。第三种:当前不存在MXD文件,而是将当前地图内容保存为一个MXD文件。前两种情况代码较为简单,第三种代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // 添加shp文件
        private void btnLoad_Click(object sender, EventArgs e)
        {
            axMapControl1.AddShapeFile(@"C:\Users\DSF\Desktop\", @"osm.shp");
        }

        // 保存为MXD文件
        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "保存MXD文件";
            saveFileDialog.Filter = "地图文件(*.mxd)|*.mxd";
            saveFileDialog.OverwritePrompt = true;
            saveFileDialog.RestoreDirectory = true;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                IMxdContents pMxdContents = axMapControl1.Map as IMxdContents;
                IMapDocument pMapDocument = new MapDocument();
                pMapDocument.New(saveFileDialog.FileName);
                pMapDocument.ReplaceContents(pMxdContents);
                pMapDocument.Save();
            }
        }
    }
}

运行结果如下:
在这里插入图片描述在这里插入图片描述

转载自:https://blog.csdn.net/HerryDong/article/details/84849981

You may also like...