界面如图所示,
button1:打开待切图
button2:关闭窗体
saveFileDialog1:保存对话框
mapcontrol1:地图
LicenseControl1:license控件
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.esriSystem;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.SpatialAnalyst;
using ESRI.ArcGIS.GeoAnalyst;
namespace cut
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static void RasterClip(IRasterLayer pRasterLayer, IPolygon clipGeo, string FileName)
{
try
{
IRaster pRaster = pRasterLayer.Raster;
IRasterProps pProps = pRaster as IRasterProps;
object cellSizeProvider = pProps.MeanCellSize().X;
IGeoDataset pInputDataset = pRaster as IGeoDataset;
IExtractionOp pExtractionOp = new RasterExtractionOpClass();
IRasterAnalysisEnvironment pRasterAnaEnvir = pExtractionOp as IRasterAnalysisEnvironment;
pRasterAnaEnvir.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSizeProvider);
object extentProvider = clipGeo.Envelope;
object snapRasterData = Type.Missing;
pRasterAnaEnvir.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref extentProvider, ref snapRasterData);
IGeoDataset pOutputDataset = pExtractionOp.Polygon(pInputDataset, clipGeo as IPolygon, true);//裁切操作
IRaster clipRaster; //裁切后得到的IRaster
if (pOutputDataset is IRasterLayer)
{
IRasterLayer rasterLayer = pOutputDataset as IRasterLayer;
clipRaster = rasterLayer.Raster;
}
else if (pOutputDataset is IRasterDataset)
{
IRasterDataset rasterDataset = pOutputDataset as IRasterDataset;
clipRaster = rasterDataset.CreateDefaultRaster();
}
else if (pOutputDataset is IRaster)
{
clipRaster = pOutputDataset as IRaster;
}
else
{
return;
}
//保存裁切后得到的clipRaster
//如果直接保存为img影像文件
IWorkspaceFactory pWKSF = new RasterWorkspaceFactoryClass();
IWorkspace pWorkspace = pWKSF.OpenFromFile(System.IO.Path.GetDirectoryName(FileName), 0);
ISaveAs pSaveAs = clipRaster as ISaveAs;
pSaveAs.SaveAs(System.IO.Path.GetFileName(FileName), pWorkspace, "IMAGINE Image");
MessageBox.Show("成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch(Exception exp)
{
MessageBox.Show("失败!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
IGeometry clipGeo = this.axMapControl1.TrackPolygon(); //
ILayer layer = this.axMapControl1.get_Layer(0); //要裁切的影像图层
IRasterLayer pRasterLayer = layer as IRasterLayer;
string fileName = "";
saveFileDialog1.Filter = "图像 (*.jpg)|*.jpg";
//saveFileDialog1.Filter = "图像 (*.shp)|*.shp";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
fileName = saveFileDialog1.FileName.ToString();
}
RasterClip(pRasterLayer, clipGeo as IPolygon, fileName );
}
/// <summary>
/// 打开图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
int currentLayerCount = this.axMapControl1.LayerCount;
ICommand pCommand = new ControlsAddDataCommandClass();
pCommand.OnCreate(this.axMapControl1.Object);
pCommand.OnClick();
IMap pMap = this.axMapControl1.Map;
}
/// <summary>
/// 关闭窗体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
还有可能出现问题,提示“当前liscense不支持spacial analysis“之类的错误
解决办法是在liscense属性中,勾选spacial analysis选项即可
本例子在XP+vs2005环境下成功。
来源:https://blog.csdn.net/guzicheng1990/article/details/99406951