打开和保存mapcontrol中的地图

本文详细介绍使用C#在ArcGIS环境下如何打开、保存及另存地图文档(.mxd)。通过实例代码,展示了如何利用ESRI.ArcGIS库进行地图文档的管理和编辑,包括对话框设置、文档路径判断及保存状态检查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#region 一些相关引用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.SystemUI;

#endregion


namespace mapcontrol
{
    public partial class Form1 : Form
    {
        //定义的全局变量
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        private System.Windows.Forms.SaveFileDialog saveFileDialog1;
        private IMapDocument m_MapDocument;
       
     
        public Form1()
        {
            InitializeComponent();
        }
            

       private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            //打开对话框的设置
            openFileDialog1= new OpenFileDialog() ;
            openFileDialog1.Title = "Open Map Document";
            openFileDialog1.Filter = "Map Documents (*.mxd)|*.mxd";
            openFileDialog1.ShowDialog();

            // 判断打开路径是否为空
            string sFilePath = openFileDialog1.FileName;
            if (sFilePath == "")
            {
                return;
            }

            //打开文档,调用OpenDocument()函数
            OpenDocument((sFilePath));  
        }
        private void OpenDocument(string sFilePath)
        {
            //创建m_Mapdocument
            m_MapDocument = new MapDocumentClass();
            m_MapDocument.Open(sFilePath, "");
            //遍历每个map对象
            for (int i = 0; i < m_MapDocument.MapCount; i++)
            {
                axMapControl1.Map = m_MapDocument.get_Map(i);
               
            }

        }

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveDocument();                   //调用SaveDocument 函数
        }

       

        private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //打开保存对话框
            saveFileDialog1=new SaveFileDialog();
            saveFileDialog1.Title = "保存地图文档";
            saveFileDialog1.Filter = "Map Documents (*.mxd)|*.mxd";
            saveFileDialog1.ShowDialog();

            //判断打开路径是否为空
            string sFilePath = saveFileDialog1.FileName;
            if (sFilePath == "")
            {
                return;
            }

            if (sFilePath == m_MapDocument.DocumentFilename)
            {
                //保存变更后文档
                SaveDocument();
            }
            else
            {
                //另存
                m_MapDocument.SaveAs(sFilePath, true, true);
                //打开另存文件
                OpenDocument((sFilePath));
                MessageBox.Show("保存成功!");
            }
        }

        private void SaveDocument()
        {
            //判断文档是否为只读文件

            if (m_MapDocument.get_IsReadOnly(m_MapDocument.DocumentFilename) == true)
            {
                MessageBox.Show("这个地图文档是只读文件!");
                return;
            }
            //保存
            m_MapDocument.Save(m_MapDocument.UsesRelativePaths, true);
            MessageBox.Show("保存成功!");
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ipqchase85

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值