这是我们GIS课程的结课作业,只能说,大心血
目录
链接在最后,但是还是希望大家可以看看代码,可以留言相互交流学习。
添加图名
指北针
比例尺(类似指北针。从样式库里添加)
图例
指北针窗体
导出地图
打印地图
public void AddTitle(IPageLayout pageLayout, String s)
{
//找到PageLayout
IPageLayout pPageLayout = mainPageLayoutControl1.PageLayout;
//找到元素容器
IGraphicsContainer pGraphicsContainer = pPageLayout as IGraphicsContainer;
//创建元素
ITextElement pTextElement = new TextElementClass();
pTextElement.Text = s;
ITextSymbol pTextSymbol = new TextSymbolClass();//Text的符号样式
pTextSymbol.Size = 30;
pTextSymbol.Color = GetRGB(0, 0, 0);
pTextElement.Symbol = pTextSymbol;
//设置位置
IElement pElement = pTextElement as IElement;
pElement.Geometry = mainPageLayoutControl1.TrackRectangle();
//将元素添加到容器中
pGraphicsContainer.AddElement(pElement, 0);
//刷新
mainPageLayoutControl1.Refresh();
}
IMapFrame pMapFrame = ((IActiveView)this.mainPageLayoutControl1.PageLayout).GraphicsContainer.FindFrame(this.mainPageLayoutControl1.ActiveView.FocusMap) as IMapFrame;
IElement pElement = pMapFrame as IElement;
UID pID = new UIDClass();
pID.Value = "esriCarto.MarkerNorthArrow";
IEnvelope pEnv = new EnvelopeClass();
pEnv.PutCoords(16, 24, 22, 26);
AddNorthArrow NForm = new AddNorthArrow(mainPageLayoutControl1.ActiveView, esriSymbologyStyleClass.esriStyleClassNorthArrows);//axPageLayoutControl1.ActiveView, esriSymbologyStyleClass.esriStyleClassNorthArrows
NForm.ShowDialog();
IStyleGalleryItem styleGalleryItem = NForm.m_styleGalleryItem;
NForm.Dispose();
if (styleGalleryItem == null) return;
IMapSurround pMapSurround = this.CreateSurround(pID, pEnv, styleGalleryItem, pMapFrame, this.mainPageLayoutControl1.PageLayout);
this.mainPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
IMapFrame pMapFrame = ((IActiveView)this.mainPageLayoutControl1.PageLayout).GraphicsContainer.FindFrame(this.mainPageLayoutControl1.ActiveView.FocusMap) as IMapFrame;
IElement pElement = pMapFrame as IElement;
UID pID = new UIDClass();
pID.Value = "esriCarto.ScaleLine";
IEnvelope pEnv = new EnvelopeClass();
pEnv.PutCoords(2, 3, 3, 5);
AddScalebar SForm = new AddScalebar(mainPageLayoutControl1.ActiveView, esriSymbologyStyleClass.esriStyleClassScaleBars);
SForm.ShowDialog();
IStyleGalleryItem styleGalleryItem = SForm.m_styleGalleryItem;
SForm.Dispose();
if (styleGalleryItem == null) return;
IMapSurround pMapSurround = this.CreateSurround(pID, pEnv, styleGalleryItem, pMapFrame, this.mainPageLayoutControl1.PageLayout);
this.mainPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
public void AddLegend(IPageLayout pageLayout)
{
//获取axPageLayoutControl1的图形容器
IGraphicsContainer graphicsContainer = mainPageLayoutControl1.GraphicsContainer;
//获取axPageLayoutControl1空间里面显示的地图图层
IMapFrame mapFrame = (IMapFrame)graphicsContainer.FindFrame(mainPageLayoutControl1.ActiveView.FocusMap);
if (mapFrame == null) return;
//创建图例
UID uID = new UIDClass();//创建UID作为该图例的唯一标识符,方便创建之后进行删除、移动等操作
uID.Value = "esriCarto.Legend";
IMapSurroundFrame mapSurroundFrame = mapFrame.CreateSurroundFrame(uID, null);
if (mapSurroundFrame == null) return;
if (mapSurroundFrame.MapSurround == null) return;
mapSurroundFrame.MapSurround.Name = "Legend";
IEnvelope envelope = new EnvelopeClass();
envelope.PutCoords(16, 3, 20, 5);//设置图例摆放位置(原点在axPageLayoutControl左下角)
IElement element = (IElement)mapSurroundFrame;
element.Geometry = envelope;
//将图例转化为几何要素添加到axPageLayoutControl1,并刷新页面显示
mainPageLayoutControl1.AddElement(element, Type.Missing, Type.Missing, "Legend", 0);
mainPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
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;
namespace WindowsFormsApplication3
{
public partial class AddNorthArrow : Form
{
private IActiveView mActiveView;
private IFeatureLayer mFeatureLayer;
public IStyleGalleryItem m_styleGalleryItem;
private esriSymbologyStyleClass mStyleClass;
public AddNorthArrow(IFeatureLayer pLayer, IActiveView pActiveView)
{
InitializeComponent();
this.Text = "指北针样式选择";
mFeatureLayer = pLayer;
mActiveView = pActiveView;
}
public AddNorthArrow(IActiveView pActiveView, esriSymbologyStyleClass styleClass)
{
mActiveView = pActiveView;
mStyleClass = styleClass;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
axSymbologyControl1.LoadStyleFile("D:\。。。。\ESRI.ServerStyle");
axSymbologyControl1.StyleClass = esriSymbologyStyleClass.esriStyleClassNorthArrows;
//Select the color ramp item
axSymbologyControl1.GetStyleClass(axSymbologyControl1.StyleClass).SelectItem(0);
}
private void axSymbologyControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.ISymbologyControlEvents_OnMouseDownEvent e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
}
private void button2_Click(object sender, EventArgs e)
{
m_styleGalleryItem = null;
//hide the form
this.Hide();
}
private void axSymbologyControl1_OnItemSelected(object sender, ISymbologyControlEvents_OnItemSelectedEvent e)
{
m_styleGalleryItem = (IStyleGalleryItem)e.styleGalleryItem;
}
}
}
private void btnPrint_Click(object sender, EventArgs e)//点击打印Button响应
{
//创建保存文件对话框的实例
SaveFileDialog SaveFile = new SaveFileDialog();
//根据掩码文本框对对话框初始化
switch (comboBox1.SelectedIndex)
{
case 0:
SaveFile.Filter = "BPM文件(*.bmp)|*.bmp";
SaveFile.Title = "保存bmb文件";
break;
case 2:
SaveFile.Filter = "GIF文件(*.gif)|*.gif";
SaveFile.Title = "保存gif文件";
break;
case 3:
SaveFile.Filter = "JPG文件(*.jpeg)|*.jpeg";
SaveFile.Title = "保存jpg文件";
break;
case 4:
SaveFile.Filter = "PNG文件(*.png)|*.png";
SaveFile.Title = "保存png文件";
break;
case 5:
SaveFile.Filter = "TIFF文件(*.TIFF)|*.TIFF";
SaveFile.Title = "保存tiff文件";
break;
case 6:
SaveFile.Filter = "PDF文件(*.PDF)|*.pdf";
SaveFile.Title = "保存pdf文件";
break;
}
if (txtResolution.Value > 1)
{
if (SaveFile.ShowDialog() == DialogResult.OK)
{
//打开保存文件对话框 并选择路径和文件名
SaveFile.AddExtension = true;//设置为自动补充文件扩展名
IExport pExport;
switch (comboBox1.SelectedIndex)//根据掩码文本框的索引值穿件不同的pExport实例并调用Export方法
{
case 0:
pExport = new ExportBMPClass();
Export(pExport, SaveFile.FileName);
break;
case 2:
pExport = new ExportGIFClass();
Export(pExport, SaveFile.FileName);
break;
case 3:
pExport = new ExportJPEGClass();
Export(pExport, SaveFile.FileName);
break;
case 4:
pExport = new ExportPNGClass();
Export(pExport, SaveFile.FileName);
break;
case 5:
pExport = new ExportTIFFClass();
Export(pExport, SaveFile.FileName);
break;
case 6:
pExport = new ExportPDFClass();
Export(pExport, SaveFile.FileName);
break;
}
}
}
else {
MessageBox.Show("请输入有效数字~!", "提示", MessageBoxButtons.OK);
}
}
//输出文件
public void Export(IExport pExport, string path)
{
IActiveView pActiveView;
IEnvelope pPixelBoundsEnv;
int iOutputResolution;
int iScreenResolution;
int hdc;
pActiveView = m_Map.ActiveView;
pExport.ExportFileName = path;
iScreenResolution = 96;
iOutputResolution = 300;
pExport.Resolution = iOutputResolution;
tagRECT pExportFrame;
pExportFrame = pActiveView.ExportFrame;
tagRECT exportRECT;
exportRECT.left = 0;
exportRECT.top = 0;
exportRECT.right = pActiveView.ExportFrame.right * (iOutputResolution / iScreenResolution);
exportRECT.bottom = pActiveView.ExportFrame.bottom * (iOutputResolution / iScreenResolution);
pPixelBoundsEnv = new EnvelopeClass();
pPixelBoundsEnv.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom);
pExport.PixelBounds = pPixelBoundsEnv;
hdc = pExport.StartExporting();
pActiveView.Output(hdc, (int)pExport.Resolution, ref exportRECT, null, null);
pExport.FinishExporting();
pExport.Cleanup();
MessageBox.Show("成功保存地图~!", "保存", MessageBoxButtons.OK);
}
AxPageLayoutControl m_Map;//新建axPageLayoutControl变量,用于接收传进来的参数
private PageSetupDialog pageSetup=new PageSetupDialog();
PrintDocument PD = new PrintDocument();
private PrintDialog print=new PrintDialog();
public PrintMap(AxPageLayoutControl m_Map)
{
InitializeComponent();
this.Text = "打印地图";
this.m_Map = m_Map;
copy(m_Map);
// copyMap(m_Map,axPageLayoutControl1);
m_Map.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
}
/* public void copyMap(AxPageLayoutControl m_Map, AxPageLayoutControl axPageLayoutControl)
{
IObjectCopy objectCopy = new ObjectCopy();
object tocopyMap = m_Map.ActiveView.GraphicsContainer;
object copiedMap = objectCopy.Copy(tocopyMap);
object toOverwiteMap = axPageLayoutControl1.ActiveView.FocusMap;
objectCopy.Overwrite(copiedMap, ref toOverwiteMap);
}
*/
private void PrintMap_Load(object sender, EventArgs e)
{
pageSetup = new PageSetupDialog();
//初始化页面设置对话框的页面设置属性为缺省设置
pageSetup.PageSettings = new System.Drawing.Printing.PageSettings();
//初始化页面设置对话框的打印机属性为缺省设置
pageSetup.PrinterSettings = new System.Drawing.Printing.PrinterSettings();
print = new PrintDialog();
}
private void copy(AxPageLayoutControl mainlayoutControl)
{
IObjectCopy objectcopy = new ObjectCopyClass();
object tocopymap = mainlayoutControl.ActiveView.GraphicsContainer; //获取mapcontrol中的map 这个是原始的
object copiedmap = objectcopy.Copy(tocopymap); //复制一份map,是一个副本
object tooverwritemap = axPageLayoutControl1.ActiveView.GraphicsContainer; //IActiveView.FocusMap : The map that tools and controls act on. 控件和工具作用的地图,大概是当前地图吧!!!
objectcopy.Overwrite(copiedmap, ref tooverwritemap);
System.Runtime.InteropServices.Marshal.ReleaseComObject(objectcopy);
IGraphicsContainer mainGraphCon = tooverwritemap as IGraphicsContainer;
mainGraphCon.Reset();
IElement pElement = mainGraphCon.Next();
IArray pArray = new ArrayClass();
while (pElement != null)
{
pArray.Add(pElement);
pElement = mainGraphCon.Next();
}
int pElementCount = pArray.Count;
IPageLayout PrintPageLayout = axPageLayoutControl1.PageLayout;
IGraphicsContainer PrintGraphCon = PrintPageLayout as IGraphicsContainer;
PrintGraphCon.Reset();
IElement pPrintElement = PrintGraphCon.Next();
while (pPrintElement != null)
{
PrintGraphCon.DeleteElement(pPrintElement);
pPrintElement = PrintGraphCon.Next();
}
for (int i = 0; i < pArray.Count; i++)
{
PrintGraphCon.AddElement(pArray.get_Element(pElementCount - 1 - i) as IElement, 0);
}
axPageLayoutControl1.Refresh();
}
private void button1_Click(object sender, EventArgs e)
{
//实例化打印设置窗口
DialogResult result = pageSetup.ShowDialog();
//设置打印文档对象的打印机
PD.PrinterSettings = pageSetup.PrinterSettings;
//设置打印文档对象的页面设置为用户在打印设置对话框中的设置
PD.DefaultPageSettings = pageSetup.PageSettings;
//页面设置
int i;
IEnumerator paperSizes = pageSetup.PrinterSettings.PaperSizes.GetEnumerator();
paperSizes.Reset();
for (i = 0; i < pageSetup.PrinterSettings.PaperSizes.Count; ++i)
{
paperSizes.MoveNext();
if (((PaperSize)paperSizes.Current).Kind == PD.DefaultPageSettings.PaperSize.Kind)
{
PD.DefaultPageSettings.PaperSize = ((PaperSize)paperSizes.Current);
}
}
//初始化纸张和打印机
IPaper paper = new PaperClass();
IPrinter printer = new EmfPrinterClass();
//关联打印机对象和纸张对象
paper.Attach(pageSetup.PrinterSettings.GetHdevmode(pageSetup.PageSettings).ToInt32(), pageSetup.PrinterSettings.GetHdevnames().ToInt32());
printer.Paper = paper;
axPageLayoutControl1.Printer = printer;
pageSetup.ShowDialog();
}
private void btnPrint_Click(object sender, EventArgs e)
{
//显示帮助按钮
print.ShowHelp = true;
print.Document = PD;
//显示打印窗口
DialogResult result = print.ShowDialog();
// 如果显示成功,则打印.
if (result == DialogResult.OK) PD.Print();
Close();
}
}
}
比较综合。截图太多,就不放了。exe可运行,放百度网盘了。一定结合百度云的窗体,运行一下再去看代码,最后的结课报告有完整的功能介绍和运行结果截图和测试,需要的可以留言或私信。
百度云链接:
链接:https://pan.baidu.com/s/1NTFdUBJGBO69SJ3CRUKWCQ 密码:tqfp