ArcGIS Engine开发之地图导出
关于地图导出的方法有很多,但是核心技术就那么一点。下面是从项目实战中总结的一部分地图导出的方法:(以全域导出和区域导出为例)
1.由于地图导出用到的函数和方法容易重复,增加了工作量故首先将其进行封装成类(ExportMap类):用到的主要接口为:IActiveView(活动视图接口)、IGeometry(几何接口)、IRgbColor(颜色接口)、IElement(要素接口)等。
具体的封装代码如下:
class ExportMap
{
#region 输出视图
public static void ExportView(IActiveView view, IGeometry pGeo, int Outputresoultion, int Width, int Height, string ExpPath, bool bRegion)
{
IExport pExport = null;
tagRECT exportrect = new tagRECT();
IEnvelope pEnvelop = pGeo.Envelope;
string sType = System.IO.Path.GetExtension(ExpPath);
switch (sType)
{
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 ".pdf":
pExport = new ExportPDFClass();
break;
default:
MessageBox.Show("没有输出格式,默认JPEG");
pExport = new ExportJPEGClass();
break;
}
pExport.ExportFileName = ExpPath;
exportrect.left = ;
exportrect.top = ;
exportrect.right = Width;
exportrect.bottom = Height;
if (bRegion)
{
view.GraphicsContainer.DeleteAllElements();
view.Refresh();
}
IEnvelope envelop = new EnvelopeClass();
envelop.PutCoords((double)exportrect.left, (double)exportrect.top, (double)exportrect.right, (double)exportrect.bottom);
pExport.PixelBounds = envelop;
view.Output(pExport.StartExporting(), Outputresoultion, ref exportrect, pEnvelop, null);
pExport.FinishExporting();
pExport.Cleanup();
}
#endregion
#region 获取RGB颜色
private static IRgbColor GetRgbColor(int intR,int intG,int intB)
{
IRgbColor pRgbColor=null;
if(intR<||intR>||intG<||intG>||intB<||intB>)
{
return pRgbColor;
}
pRgbColor=new RgbColorClass();
pRgbColor.Red=intR;
pRgbColor.Green=intG;
pRgbColor.Blue=intB;
return pRgbColor;
}
#endregion
#region 创建图形元素
/// <summary>
///
/// </summary>
/// <param name="pGeomentry">几何图形</param>
/// <param name="lineColor">边框颜色</param>
/// <param name="fillColor">填充颜色</param>
/// <returns></returns>
public static IElement CreateElement(IGeometry pGeomentry, IRgbColor lineColor, IRgbColor fillColor)
{
if (pGeomentry == null || lineColor == null || fillColor == null)
{
return null;
}
IElement pElem = null;
try
{
if (pGeomentry is IEnvelope) pElem = new RectangleElementClass();
else if (pGeomentry is IPolygon)
pElem = new PolygonElementClass();
else if (pGeomentry is ICircularArc)
{
ISegment pSegCircle = pGeomentry as ISegment;
ISegmentCollection pSegColl = new PolygonClass();
object o = Type.Missing;
pSegColl.AddSegment(pSegCircle, ref o, ref o);
IPolygon pPolygon = pSegCircle as IPolygon;
pGeomentry = pPolygon as IGeometry;
pElem = new CircleElementClass();
}
else if (pGeomentry is IPolyline)
pElem = new LineElementClass();
if (pElem == null)
return null;
pElem.Geometry = pGeomentry;
IFillShapeElement pFElem = pElem as IFillShapeElement;
ISimpleFillSymbol pSymbol = new SimpleFillSymbolClass();
pSymbol.Color=fillColor ;
pSymbol.Outline.Color=lineColor;
pSymbol.Style = esriSimpleFillStyle.esriSFSCross;//图形元素的样式
if (pSymbol == null)
{
return null;
}
pFElem.Symbol = pSymbol;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message );
}
return pElem;
}
#endregion
#region 视图窗口绘制几何图形元素
/// <summary>
///
/// </summary>
/// <param name="pGeometry">几何图形</param>
/// <param name="activeView">活动视图</param>
public static void AddElement(IGeometry pGeometry,IActiveView activeView)
{
IRgbColor fillColor=GetRgbColor(,,);
IRgbColor lineColor=GetRgbColor(,,);
IElement pEle=CreateElement(pGeometry,lineColor,fillColor );//调用图形元素的函数
IGraphicsContainer pGC = activeView.GraphicsContainer;
if (pGC != null)
{
pGC.AddElement(pEle, );
activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, pEle, null);
}
}
#endregion
#region 全域导出
/// <summary>
/// 全域导出
/// </summary>
/// <param name="OutputResolution">输出分辨率</param>
/// <param name="ExpPath">输出路径</param>
/// <param name="view">视图</param>
public static void ExportActiveView(int OutputResolution, string ExpPath, IActiveView view)
{
IExport pExport = null;
tagRECT exportRect;
IEnvelope envelop2 = view.Extent;
int num = (int)Math.Round(view.ScreenDisplay.DisplayTransformation.Resolution);
string sType = System.IO.Path.GetExtension(ExpPath);
switch (sType)
{
case ".jgp":
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 ".pdf":
pExport = new ExportPDFClass();
break;
default: MessageBox.Show("No Export Foemat,the default format is JPEG!");
pExport = new ExportJPEGClass();
break;
}
pExport.ExportFileName = ExpPath;
exportRect.left = ; exportRect.top = ;
exportRect.right = (int)Math.Round((double)(view.ExportFrame.right * (((double)OutputResolution) / ((double)num))));
exportRect.bottom = (int)Math.Round((double)(view.ExportFrame.bottom * (((double)OutputResolution) / ((double)num))));
IEnvelope envelop = new EnvelopeClass();
envelop.PutCoords((double)exportRect.left, (double)exportRect.top, (double)exportRect.right, (double)exportRect.bottom);
pExport.PixelBounds = envelop;
view.Output(pExport.StartExporting(), OutputResolution, ref exportRect, envelop2, null);
pExport.FinishExporting();
pExport.Cleanup();
}
#endregion
#region 区域导出
/// <summary>
/// 区域导出
/// </summary>
/// <param name="pGeo">输出的图形</param>
/// <param name="OutputResolution">输出的范围</param>
/// <param name="ExpPath">输出路径</param>
/// <param name="view">视图</param>
public static void ExportRegion(IGeometry pGeo, int OutputResolution, string ExpPath, IActiveView view)
{
IExport export = null;
IWorldFileSettings setting = null;
IEnvelope envelope2 = pGeo.Envelope;
string str = ExpPath.Substring(ExpPath.Length - , ).ToUpper();
switch (str)
{
case "JPG":
setting = new ExportJPEGClass();
export = new ExportJPEGClass();
setting = export as IWorldFileSettings;
setting.MapExtent = envelope2;
setting.OutputWorldFile = false;
break;
case "BMP":
setting = new ExportBMPClass();
export = new ExportBMPClass();
setting = export as IWorldFileSettings;
setting.MapExtent = envelope2;
setting.OutputWorldFile = false;
break;
case "TIF":
setting = new ExportTIFFClass();
export = new ExportTIFFClass();
setting = export as IWorldFileSettings;
setting.MapExtent = envelope2;
setting.OutputWorldFile = false;
break;
case "PNG":
setting = new ExportPNGClass();
export = new ExportPNGClass();
setting = export as IWorldFileSettings;
setting.MapExtent = envelope2;
setting.OutputWorldFile = false;
break;
default: break;
}
if (setting == null)
{
export.ExportFileName = ExpPath;
int num = (int)Math.Round(view.ScreenDisplay.DisplayTransformation.Resolution);
tagRECT grect2 = new tagRECT();//实例化矩形
IEnvelope envelop3 = new EnvelopeClass();
view.ScreenDisplay.DisplayTransformation.TransformRect(envelope2, ref grect2, );
grect2.left = ;
grect2.top = ;
grect2.right = (int)Math.Round((double)(grect2.right - grect2.left) * (((double)OutputResolution) / ((double)num)));
grect2.bottom = (int)Math.Round((double)((grect2.bottom - grect2.top) * (((double)OutputResolution) / ((double)num))));
envelop3.PutCoords((double)grect2.left, (double)grect2.top, (double)grect2.right, (double)grect2.bottom);
export.PixelBounds = envelop3;
view.GraphicsContainer.DeleteAllElements();
view.Output(export.StartExporting(), OutputResolution, ref grect2, envelope2, null);
export.FinishExporting();
export.Cleanup();
AddElement(pGeo, view);
} }
#endregion
}
2.添加输出设置窗体,分别有,输出图片的高、宽、分辨率、输出保存路径、导出按钮。

具体的设置代码如下:
public partial class ExportMapForm : DevExpress.XtraEditors.XtraForm
{
//定义全局变量
private string pSavePath = "";
private IActiveView pActiveView;
private IGeometry pGeometry = null;
#region 只读属性,地图导出空间图形
public IGeometry GetGeometry
{
set { pGeometry = value; }
}
private bool bRegion = true;
#endregion
/// <summary>
/// 只读属性,是全域导出还是区域导出
/// </summary>
public bool IsRegion
{
set { bRegion = value; }
} //获取主窗口的MapControl控件
public ExportMapForm(AxMapControl mainAxMapControl)
{
InitializeComponent(); pActiveView = mainAxMapControl.ActiveView; } private void ExportMapForm_Load(object sender, EventArgs e)
{ }
#region 输入窗口的大小
private void InitFormSize()
{
cboResoultion.Text = pActiveView.ScreenDisplay.DisplayTransformation.Resolution.ToString();
cboResoultion.Items.Add(cboResoultion.Text);
if (bRegion)
{
IEnvelope pEnvelop = pGeometry.Envelope;
tagRECT pRECT = new tagRECT();
pActiveView.ScreenDisplay.DisplayTransformation.TransformRect(pEnvelop, ref pRECT,);
if (cboResoultion.Text != "")
{
txtWidth.Text = pRECT.right.ToString();
txtHeight.Text = pRECT.bottom.ToString();
}
}
else
{
if (cboResoultion.Text != "")
{
txtWidth.Text = pActiveView.ExportFrame.right.ToString();
txtHeight.Text = pActiveView.ExportFrame.bottom.ToString();
}
}
}
#endregion
#region combox的ChangeIndex事件
private void cboResoultion_SelectedIndexChanged(object sender, EventArgs e)
{
double num = (int)Math.Round(pActiveView.ScreenDisplay.DisplayTransformation.Resolution);
if (cboResoultion.Text == "")
{
txtWidth.Text = "";
txtHeight.Text = "";
return;
}
if (bRegion)
{
IEnvelope pEnvelop = pGeometry.Envelope;
tagRECT pRECT = new tagRECT();
pActiveView.ScreenDisplay.DisplayTransformation.TransformRect(pEnvelop, ref pRECT,);
if (cboResoultion.Text != "")
{
txtWidth.Text = Math.Round((double)(pRECT.right * (double.Parse(cboResoultion.Text) / (double)num))).ToString();
}
}
else
{
txtWidth.Text = Math.Round((double)(pActiveView.ExportFrame.right * (double.Parse(cboResoultion.Text) / (double)num))).ToString();
txtHeight.Text = Math.Round((double)(pActiveView.ExportFrame.bottom * (double.Parse(cboResoultion.Text) / (double)num))).ToString();
}
}
#endregion
#region 保存按钮的单击事件
private void btnExPath_Click(object sender, EventArgs e)
{
SaveFileDialog sfdExportMap = new SaveFileDialog();
sfdExportMap.DefaultExt = "jpg|bmp|gif|tif|png|pdf";
sfdExportMap.Filter = "JPGE 文件(*.jpg)|*.jpg|BMP 文件(*.bmp)|*.bmp|GIF 文件(*.gif)|*.gif|TIF 文件(*.tif)|*.tif|PNG 文件(*.png)|*.png|PDF 文件(*.pdf)|*.pdf";
sfdExportMap.OverwritePrompt = true;//重复写入时提示错误
sfdExportMap.Title = "保存为";
txtExPath.Text = "";
if (sfdExportMap.ShowDialog() != DialogResult.Cancel)
{
pSavePath = sfdExportMap.FileName;
txtExPath.Text = sfdExportMap.FileName;
}
}
#endregion
#region 导出按钮单击事件
private void btnExPort_Click(object sender, EventArgs e)
{
if (txtExPath.Text == "")
{
MessageBox.Show("请先确定导出路径!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else if (cboResoultion.Text == "")
{
if (txtExPath.Text == "")
{
MessageBox.Show("请输入分辨率!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
else if (Convert.ToInt16(cboResoultion.Text) == )
{
MessageBox.Show("请正确输入分辨率!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
try
{
int resoultion = int.Parse(cboResoultion.Text);//输出图片的分辨率
int width = int.Parse(cboResoultion.Text);//输出图片的宽度
int height = int.Parse(cboResoultion.Text);//输出图片的高度
ExportMap.ExportView(pActiveView, pGeometry, resoultion, width, height, pSavePath, bRegion);
pActiveView.GraphicsContainer.DeleteAllElements();
pActiveView.Refresh();
MessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception)
{
MessageBox.Show("导出失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
#endregion
#region 取消按钮的单击事件
private void btnCancel_Click(object sender, EventArgs e)
{
//局部导出时没有导出图像就退出
pActiveView.GraphicsContainer.DeleteAllElements();
pActiveView.Refresh();
Dispose();
}
#endregion
#region 图片导出窗口Close事件
private void ExportMapForm_FormClosed(object sender, FormClosedEventArgs e)
{
//局部导出时没有导出图像就关闭
pActiveView.GraphicsContainer.DeleteAllElements();
pActiveView.Refresh();
Dispose(); }
#endregion
3.在main窗体中进行实例化输出窗体:private ExportMapForm frmExpMap = null;
4.在MainMap Control_OnMouseDown中进行写入case:(只适合区域导出,单击选择边框)
#region 地图的区域导出
case "ExportRegion":
//删除视图中的数据
mainMapControl.ActiveView.GraphicsContainer.DeleteAllElements();
mainMapControl.ActiveView.Refresh();
IPolygon pPolygon = DrawPolygon(mainMapControl);
if (pPolygon == null) return;
ExportMap.AddElement(pPolygon, mainMapControl.ActiveView);
if (frmExpMap == null || frmExpMap.IsDisposed)
{
frmExpMap = new ExportMapForm(mainMapControl);
}
frmExpMap.IsRegion = true;
frmExpMap.GetGeometry = pPolygon as IGeometry;
frmExpMap.Show();
frmExpMap.Activate();
break;
#endregion
5.全域导出和区域导出按钮的单击事件代码:
#region 地图导出之全域导出
private void btnExportMap_ItemClick(object sender, ItemClickEventArgs e)
{
if (frmExpMap == null || frmExpMap.IsDisposed)
{
frmExpMap = new ExportMapForm(mainMapControl);
}
frmExpMap.IsRegion = false;
frmExpMap.GetGeometry = mainMapControl.ActiveView.Extent;
frmExpMap.Show();
frmExpMap.Activate();
}
#endregion
#region 地图导出之区域导出
private void btnExportRegionMap_ItemClick(object sender, ItemClickEventArgs e)
{
mainMapControl.CurrentTool = null;
mainMapControl.MousePointer = esriControlsMousePointer.esriPointerCrosshair;
pMouseOperate = "ExportRegion";
}
#endregion
ArcGIS Engine开发之地图导出的更多相关文章
- ArcGIS Engine开发之地图基本操作(4)
ArcGIS Engine开发中数据库的加载 1.加载个人地理数据库数据 个人地理数据库(Personal Geodatabase)使用Miscrosoft Access文件(*.mdb)进行空间数据 ...
- ArcGIS Engine开发之地图基本操作(3)
地图数据的加载 一.加载Shapefile数据 Shapefile文件是目前主流的一种空间数据的文件存储方式,也是不同GIS软件进行数据格式转换常用的中间格式.加载Shapefile数据的方式有两种: ...
- ArcGIS Engine开发之地图基本操作(2)
地图数据的加载 1.加载地图文档 ArcGIS Engine支持加载多种类型的数据,有矢量数据的Coverage.Shapefile.dwg/dxf文件,栅格数据的BMP.GRID.控件数据库等.很多 ...
- ArcGIS Engine开发之地图基本操作(1)
ArcGIS提供的各类数据形式以及相应接口 1. 空间数据 在GIS软件中,空间数据有多种不同的形式存在.按照不同的划分标准可以分为矢量数据和栅格数据.GIS格式数据和非GIS格式数据(CAD格式). ...
- ArcGIS Engine开发之地图浏览
地图的浏览功能包括缩放.移动.量测旋转等. 1.放大与缩小 无论是放大还是缩小,都是通过改变MapControl中当前视图的范围Extent属性来实现的,主要用到包络线(Envelope)类. 包络线 ...
- ArcGIS Engine开发前基础知识(2)
ArcGIS基本控件简介 ArcGIS Engine控件是一组可视化的开发组件,每个ArcGIS Engine控件都是一个COM组件.这些组件包括MapControl,PageLayoutContro ...
- C#,ArcGIS Engine开发入门教程
C#,ArcGIS Engine开发入门教程 转自:http://blog.csdn.net/yanleigis/article/details/2233674 目录(?)[+] 五实现 一 加载A ...
- ArcGIS Engine开发基础总结(一)
标准Engine功能 地图浏览 地图制作 数据查询 数据分析 及 所有的开发控件 —MapControl, PageLayout, Toolbar, TOC, ArcReader 对所有矢量和栅 ...
- ArcGIS Engine开发鹰眼图的功能(基础篇)
鹰眼是用于调节全视域范围内主地图显示范围情况的副地图.它体现了地图整体与详细局部的关系. 用户可以通过鼠标单击或者画框等动作实现鹰眼与主地图的交互情况. 鹰眼功能的原理是通过主地图窗口的地图控件和鹰眼 ...
随机推荐
- 【swift】BlockOperation和GCD实用代码块
//BlockOperation // // ViewController.swift import UIKit class ViewController: UIViewController { @I ...
- 微软收购Xamarin,你怎么看?
今天的最大新闻就是微软收购热门初创企业Xamarin,从网上的反馈大部分都是积极的,也有担心微软在把Xamarin移动开发技术整合进VS的同时,还很有可能废掉MONO的GUI客户端能力只保留.net ...
- 初探领域驱动设计(2)Repository在DDD中的应用
概述 上一篇我们算是粗略的介绍了一下DDD,我们提到了实体.值类型和领域服务,也稍微讲到了DDD中的分层结构.但这只能算是一个很简单的介绍,并且我们在上篇的末尾还留下了一些问题,其中大家讨论比较多的, ...
- IIS访问共享文件详解
前言 公司同事做了一个报表系统,需要做集群部署,本来是一件挺容易的事,但是部署过程中却遇到啦种种蛋疼问题. 问题1.我们的报表使用的是微软的水晶报表,需要上传报表的配置文件,然后水晶报表提供的控件来读 ...
- Mono的简单例子
一直对移动端开发有些兴趣,但苦于不会Java,好在终于找到了个好玩的. 安装方法略了,先建立一个玩玩 不多说,贴代码了,需要注意的只有些JAVA和C#写法不太一样的地方,不细介绍了,因为没什么经验,乱 ...
- JavaScript资源大全中文版(Awesome最新版)
Awesome系列的JavaScript资源整理.awesome-javascript是sorrycc发起维护的 JS 资源列表,内容包括:包管理器.加载器.测试框架.运行器.QA.MVC框架和库.模 ...
- 学习ASP.NET Core,你必须了解无处不在的“依赖注入”
ASP.NET Core的核心是通过一个Server和若干注册的Middleware构成的管道,不论是管道自身的构建,还是Server和Middleware自身的实现,以及构建在这个管道的应用,都需要 ...
- NPOI导出Excel
using System;using System.Collections.Generic;using System.Linq;using System.Text;#region NPOIusing ...
- 前端MVC学习总结(二)——AngularJS验证、过滤器、指令
一.验证 angularJS中提供了许多的验证指令,可以轻松的实现验证,只需要在表单元素上添加相应的ng属性,常见的如下所示: <input Type="text" ng-m ...
- linux内核数据结构之kfifo
1.前言 最近项目中用到一个环形缓冲区(ring buffer),代码是由linux内核的kfifo改过来的.缓冲区在文件系统中经常用到,通过缓冲区缓解cpu读写内存和读写磁盘的速度.例如一个进程A产 ...