地图导出为图片是一种将地图保存为图片格式文件的方法,它可以方便地在其他软件中进行浏览和打印,而无需打印出纸质地图。本系统设计使用了ExportMap类来实现局部导出功能,通过ExportMap类提供的这些各具特色的功能,用户可以轻松地将想要的地图内容截取出来,并进行灵活的处理和编辑。计算公式为:
具体逻辑为:
- 引入了 ESRI ArcGIS 的相关类库,用于地图的可视化、数据访问和分析等功能。
- 定义了一个名为
FmExportMap
的窗体类,用于地图导出操作。 - 类中定义了私有字段
pSavePath
用于保存导出路径,pActiveView
用于获取地图的当前视图,pGeometry
用于存储地图的几何要素,bRegion
用于标识是否为区域导出。 - 构造函数
FmExportMap
接受一个AxPageLayoutControl
参数,初始化窗体,并设置pActiveView
。 - 实现了窗体加载事件
FormExportMap_Load
,在加载窗体时初始化窗体大小。 - 实现了导出按钮
btnExport_Click
的点击事件,首先检查是否设置了导出路径和分辨率,然后调用ExportMap.ExportView
方法执行地图导出操作。 - 实现了选择导出路径按钮
btnExPath_Click
的点击事件,使用SaveFileDialog
获取用户选择的导出路径,并更新pSavePath
。 - 实现了分辨率下拉框
cboResolution_SelectedIndexChanged
的选中事件,根据选中的分辨率计算并更新导出图片的宽度和高度。 - 实现了取消按钮
btnCancel_Click
和窗体关闭事件FormExportMap_FormClosed
,用于关闭当前窗体。 - 其他一些辅助方法和事件处理方法,如
InitFormSize
用于初始化窗体大小,txtExPath_TextChanged
用于处理导出路径文本框内容变化事件等。
主要代码为:
private void btnExport_Click(object sender, EventArgs e)
{
if (txtExPath.Text == "")
{
MessageBox.Show("请先确定导出路径!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else if (cboResolution.Text == "")
{
if (txtExPath.Text == "")
{
MessageBox.Show("请输入分辨率!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
else if (Convert.ToInt16(cboResolution.Text) == 0)
{
MessageBox.Show("请正确输入分辨率!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
try
{
int resolution = int.Parse(cboResolution.Text); //输出图片的分辨率
int width = int.Parse(txtWidth.Text); //输出图片的宽度,以像素为单位
int height = int.Parse(txtHeight.Text); //输出图片的高度,以像素为单位
ExportMap.ExportView(pActiveView, pGeometry, resolution, width, height, pSavePath, bRegion);
MessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception)
{
MessageBox.Show("导出失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void FormExportMap_FormClosed(object sender, FormClosedEventArgs e)
{
Dispose();
}
private void btnExPath_Click(object sender, EventArgs e)
{
SaveFileDialog sfdExportMap = new SaveFileDialog();
sfdExportMap.DefaultExt = "jpg|bmp|gig|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;
}
}
private void cboResolution_SelectedIndexChanged(object sender, EventArgs e)
{
double num = (int)Math.Round(pActiveView.ScreenDisplay.DisplayTransformation.Resolution);
if (cboResolution.Text == "")
{
txtWidth.Text = "";
txtHeight.Text = "";
return;
}
if (bRegion)
{
IEnvelope pEnvelope = pGeometry.Envelope;
tagRECT pRECT = new tagRECT();
pActiveView.ScreenDisplay.DisplayTransformation.TransformRect(pEnvelope, ref pRECT, 9);
if (cboResolution.Text != "")
{
txtWidth.Text = Math.Round((double)(pRECT.right * (double.Parse(cboResolution.Text) / (double)num))).ToString();
txtHeight.Text = Math.Round((double)(pRECT.bottom * (double.Parse(cboResolution.Text) / (double)num))).ToString();
}
}
else
{
txtWidth.Text = Math.Round((double)(pActiveView.ExportFrame.right * (double.Parse(cboResolution.Text) / (double)num))).ToString();
txtHeight.Text = Math.Round((double)(pActiveView.ExportFrame.bottom * (double.Parse(cboResolution.Text) / (double)num))).ToString();
}
}