ArcGIS Engine下实现地图输出

 

ArEngine给我们提供了很多种将地图输出的方式,包括文件方式和打印等,这些方式又受不同参数的控制有着不同的效果。要想得到符合我们要求的效果,就必须了解这些方法所表达的意思和参数的内容,下面简要说明一下,其它的可以参考开发文档。

思路:采用IActiveView接口下的output方法,可以将地图输出为上十种格式,具体的格式受IExport类型限制,如ExportBMP,ExportPNG、ExportJPEG等,下面以输出为JPEG格式来说明。

首先定义ExportJPEG的实例pExport,然后设置其相关的参数,过程比较简单,这里重点描述一下相关的参数设置。

方法:OutPut(hdc, Dpi, pixelBounds, VisibleBounds, TrackCancel )

1、这里hdc是输出设备,由pExport.StartExporting指定;

2、Dpi是输出图片的精度,但是这里这个resolution并不能改变图片的精度,无论设置多大的dpi,输出同一范围图片的大小、精度都是一样的。要想改变精度,得指定IOutputRasterSettings::ResampleRatio这个参数,可以设置1—5个级别的采样率,在输出图片很大的时候这个参数能提高图片的质量;

3、PixelBounds设置的是输出像素所占的范围;

4、VisibleBounds指定地图可视的范围,这个范围是以地图坐标为单位的,以当前MapExtent为基准来控制放大、缩小视图;

5、还有个参数是pExpotrt.PixelBounds,定义的是输出图片的大小,即图片尺寸,相当于画布大小。当输出像素的范围大于图片大小的时候就会裁切图片,只输出部分地图;当它小于图片尺寸时地图会缩小到画布的一角。

综上所述,VisibleBounds或者像素范围和图片大小都可以设置地图的输出范围,一般按照具体的需要采用其中一种就可以了。

最后一点说明一下,DeviceFrame是当前设备范围,在ArcMap中就是地图所在的客户区,以左上角为起点(0,0);ExportFrame和DeviceFrame差不多,只是可能比前者略小一点;Map Extent一般是指地图范围,采用的是地图单位。整个AO体系里面有很多不同的Extent范围,弄清它们之间的关系对开发人员来说是很必要的。

 

示例:

 

1、界面如下:

2、实现步骤

a、创建新窗体,ExportMapForm

b、添加控件ComboBox,TextBox,Button等,具体参数设置如下:

TextBox

txtFileName

用于显示输出路径,ReadOnly设置为true

Button

btnSave

显示“路径”,用于设置文件导出位置

NumbericUpDown

txtResolution

选择分辨率,默认value=96

CombiBox

cmbPageSize

显示“图片尺寸”,集合:自定义大小、A4A3A2A1A0

TextBox

txtWidth

宽度

TextBox

txtLength

长度

RadioButton

radioButton1

显示“英寸”

RadioButton

radioButton2

显示“厘米”

RadioButton

radioButton3

显示“像素”

c、框体的源代码如下:

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.Geodatabase;  
using ESRI.ArcGIS.Output;  
using ESRI.ArcGIS.esriSystem;  
using ESRI.ArcGIS.Geometry;  
using System.Text.RegularExpressions;  
using ESRI.ArcGIS.Controls;  
  
  
namespace GIS  
{  
    public partial class ExportMapForm : Form  
    {  
        private double pWidth, pHeight;   
        private IActiveView pActiveView=null;  
        public ExportMapForm(IHookHelper hookHelper)  
        {  
            InitializeComponent();  
            pActiveView = hookHelper.ActiveView;  
        }  
        private void btnSave_Click(object sender, EventArgs e)  
        {  
            this.saveMapFileDialog.ShowDialog();  
            this.txtFileName.Text = saveMapFileDialog.FileName;  
        }  
  
        private void cmdOK_Click(object sender, EventArgs e)  
        {  
            this.Cursor = Cursors.WaitCursor;  
            if (!IsNumbericA(txtWidth.Text))  
            {  
                MessageBox.Show("请输入数字!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);  
                txtWidth.Focus();  
                return;  
            }  
            if (!IsNumbericA(txtHeight.Text))  
            {  
                MessageBox.Show("请输入数字!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);  
                txtHeight.Focus();  
                return;  
            }  
            if (txtResolution.Value > 1)  
            {  
                ExportTool();  
            }  
            this.Cursor = Cursors.Default;  
        }  
  
        private void ExportTool()  
        {  
             IExport pExport=null;  
//           IExportJPEG pExportFormat;  
             IWorldFileSettings pWorldFile=null;  
             IExportImage pExportType;  
             IEnvelope pDriverBounds = null;  
  //         int lScreenResolution ;  
             ESRI.ArcGIS.Display.tagRECT userRECT=new ESRI.ArcGIS.Display.tagRECT();  
             IEnvelope pEnv=new EnvelopeClass();  
 //          double dWidth;  
 //          double dHeight;  
             int lResolution;  
             lResolution = Convert.ToInt32(this.txtResolution.Value);  
            switch (this.saveMapFileDialog.Filter.ToString().Trim().Substring(0,3))  
            {  
                case "jpg":  
                    pExport = new ExportJPEGClass();  
                    break;  
                case "bmp":  
                    pExport = new ExportBMPClass();  
                    break;  
                case "gif":  
                    pExport = new ExportGIFClass();  
                    break;  
                case "tif":  
                    pExport = new ExportTIFFClass();  
                    break;  
                case "png":  
                    pExport = new ExportPNGClass();  
                    break;  
                case "emf":  
                    pExport = new ExportEMFClass();  
                    break;  
                case "pdf":  
                    pExport = new ExportPDFClass();  
                    break;  
                case ".ai":     
                    pExport = new ExportAIClass();  
                    break;  
                case "svg":  
                    pExport = new ExportSVGClass();  
                    break;  
                default:  
                    pExport = new ExportJPEGClass();  
                    break;  
            }  
       
            if (this.txtFileName.Text.ToString().Trim() != "")  
            {  
                if (System.IO.File.Exists(this.txtFileName.Text.ToString()) == true)  
                {  
                    MessageBox.Show("该文件已经存在,请重新命名!");  
                    this.txtFileName.Text = "";  
                    this.txtFileName.Focus();  
                }  
                else  
                {  
                    pExport.ExportFileName = this.txtFileName.Text;  
                    pExport.Resolution = lResolution;  
                    pExportType = pExport as IExportImage;  
                    pExportType.ImageType = esriExportImageType.esriExportImageTypeTrueColor;  
                    pEnv = pActiveView.Extent;  
                    pWorldFile = (IWorldFileSettings)pExport;  
                    pWorldFile.MapExtent = pEnv;  
                    pWorldFile.OutputWorldFile = false;  
                    userRECT.top = 0;  
                    userRECT.left = 0;  
                    userRECT.right = Convert.ToInt32(pWidth);  
                    userRECT.bottom = Convert.ToInt32(pHeight);  
                    pDriverBounds = new EnvelopeClass();  
                    pDriverBounds.PutCoords(userRECT.top, userRECT.bottom, userRECT.right, userRECT.top);  
                    pExport.PixelBounds = pDriverBounds;  
  
                    ITrackCancel pTrackCancel = new TrackCancelClass();  
                    pActiveView.Output(pExport.StartExporting(), lResolution, ref userRECT, pActiveView.Extent, pTrackCancel);  
                    pExport.FinishExporting();  
                    MessageBox.Show("打印图片保存成功!", "保存", MessageBoxButtons.OK);  
                    this.Close();  
  
                }  
  
            }  
            else  
            {  
                MessageBox.Show("请保存文件!");  
            }  
  
        }  
        public IActiveView ResActiveView  
        {  
            get {  
                return pActiveView;  
            }  
            set {  
                pActiveView = value;  
            }  
        }  
  
        private void txtResolution_ValueChanged(object sender, EventArgs e)  
        {  
             cmbPageSize_SelectedValueChanged(null,null);  
             if (this.radioButton3.Checked == true)  
             {  
                 this.txtWidth.Text = pWidth.ToString(".00");  
                 this.txtHeight.Text = pHeight.ToString(".00");  
             }  
        }  
  
        private void cmbPageSize_SelectedValueChanged(object sender, EventArgs e)  
        {  
            switch(cmbPageSize.Text)  
            {  
                case "自定义大小":  
                    txtWidth.Focus();  
                    break;  
                case "A4":  
                    showWH(21,29.7);  
                    break;  
                case"A3":  
                    showWH(29.7, 42);  
                    break;  
                case"A2":  
                    showWH(42, 59.4);  
                    break;  
                case"A1":  
                    showWH(59.4, 84.1);  
                    break;  
               case"A0":  
                    showWH(84.1, 118.9);  
                    break;  
            }      
        }  
  
//显示宽度和高度  
//传入的参数必须是厘米  
        private void showWH(double pW,double pH)  
       {  
           pWidth = Convert.ToDouble((pW / 2.54) * Convert.ToDouble(txtResolution.Value));  
           pHeight = Convert.ToDouble((pH / 2.54) * Convert.ToDouble(txtResolution.Value));  
            if (this.radioButton1.Checked == true)  
            {  
                this.txtWidth.Text = Convert.ToDouble(pW / 2.54).ToString(".00");  
                this.txtHeight.Text = Convert.ToDouble(pH / 2.54).ToString(".00");  
            }  
            else if (this.radioButton2.Checked == true)  
            {  
                this.txtWidth.Text = pW.ToString(".00");  
                this.txtHeight.Text = pH.ToString(".00");  
            }  
            else  
            {  
                this.txtWidth.Text = pW.ToString(".00");  
                this.txtHeight.Text = pH.ToString(".00");  
            }  
        }  
  
        private void txtWidth_TextChanged(object sender, EventArgs e)  
        {  
            //if (!IsNumbericA(txtWidth.Text))  
            //{  
            //    MessageBox.Show("请输入数字!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);  
            //    return;  
            //}  
            if (this.radioButton1.Checked == true)  
            {  
                pWidth = Convert.ToDouble(txtWidth.Text) * Convert.ToDouble(txtResolution.Value);  
  
            }  
            else if (this.radioButton2.Checked == true)  
            {  
                pWidth = Convert.ToDouble(Convert.ToDouble(txtWidth.Text) / 2.54) * Convert.ToDouble(txtResolution.Value);  
  
            }else  
            {  
                pWidth = Convert.ToDouble(txtWidth.Text);  
            }  
        }  
  
        private void txtHeight_TextChanged(object sender, EventArgs e)  
        {  
            //if (!IsNumbericA(txtHeight.Text))  
            //{  
            //    MessageBox.Show("请输入数字!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);  
            //    return;  
            //}  
                  
            if (this.radioButton1.Checked == true)  
            {  
                pHeight = Convert.ToDouble(txtHeight.Text) * Convert.ToDouble(txtResolution.Value);  
  
            }  
            else if (this.radioButton2.Checked == true)  
            {  
                pHeight = Convert.ToDouble(Convert.ToDouble(txtHeight.Text) / 2.54) * Convert.ToDouble(txtResolution.Value);  
  
            }  
            else  
            {  
                pHeight = Convert.ToDouble(txtHeight.Text);  
            }  
        }  
  
        private void radioButton1_Click(object sender, EventArgs e)  
        {  
            this.txtWidth.Text = Convert.ToDouble(pWidth / Convert.ToDouble(txtResolution.Value)).ToString(".00");  
            this.txtHeight.Text = Convert.ToDouble(pHeight / Convert.ToDouble(txtResolution.Value)).ToString(".00");  
        }  
  
        private void radioButton2_Click(object sender, EventArgs e)  
        {  
                 this.txtWidth.Text = Convert.ToDouble(2.54 * pWidth / Convert.ToDouble(txtResolution.Value)).ToString(".00");  
                this.txtHeight.Text =Convert.ToDouble(2.54 * pHeight / Convert.ToDouble(txtResolution.Value)).ToString(".00");  
  
        }  
  
        private void radioButton3_Click(object sender, EventArgs e)  
        {  
            this.txtWidth.Text = pWidth.ToString(".00");  
            this.txtHeight.Text = pHeight.ToString(".00");  
  
        }  
        #region 通用函数  
        /// <summary>  
        /// 是否大于0的数字  
        /// </summary>  
        /// <param name="v"></param>  
        /// <returns></returns>  
        private bool IsNumbericA(string v)  
        {  
            return ((this.IsIntegerA(v)) || (this.IsFloatA(v)));  
        }  
        /// <summary>  
        /// 是否正浮点数  
        /// </summary>  
        /// <param name="v"></param>  
        /// <returns></returns>  
        private bool IsFloatA(string v)  
        {  
            string pattern = @"^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$";  
            Regex reg = new Regex(pattern);  
            return reg.IsMatch(v);  
        }  
        /// <summary>  
        /// 是否正整数  
  
  
        /// </summary>  
        /// <param name="v"></param>  
        /// <returns></returns>  
        private bool IsIntegerA(string v)  
        {  
            string pattern = @"^[0-9]*[1-9][0-9]*$";  
            Regex reg = new Regex(pattern);  
            return reg.IsMatch(v);  
        }  
        #endregion  
  
        private void frmResource_FormClosing(object sender, FormClosingEventArgs e)  
        {  
            if (e.CloseReason == CloseReason.UserClosing)  
            {  
                e.Cancel = true;  
                this.Hide();  
            }  
        }  
    }  
}  

 

d、框体的调用:

//参数定义      
    IHookHelper layout_hookHelper = new HookHelperClass();  
//参数赋值  
    layout_hookHelper.Hook = this.axPageLayoutControl1.Object;  

ExportMapForm EMFrm = new ExportMapForm(layout_hookHelper);  
EMFrm.ShowDialog();  

 

 

 

 

  • 7
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值