第一步, 通过对话框的方式,打开路径找文件
pOpenFileDialog.CheckFileExists = true;
pOpenFileDialog.Title = "打开Raster栅格文件";
pOpenFileDialog.Filter = "栅格文件(*.*)|*.bmp;*.tif;*.jpg;*.img|(*.bmp)|*.bmp|(*.tif)|*.tif(*.jpg)|*.jpg|(*.img)*.img";
pOpenFileDialog.ShowDialog();
string RasterFileName = pOpenFileDialog.FileName;
if (RasterFileName == "")
return;
string Path = System.IO.Path.GetDirectoryName(RasterFileName); //获取文件路径
string Name = System.IO.Path.GetFileName(RasterFileName); //获取栅格文件名
//第二步,建立工作空间工厂 (工作空间工厂、工作空间、栅格工作空间)三级,然后实例化栅格要素数据集
IWorkspaceFactory pWorkspaceFactory = new RasterWorkspaceFactory();
IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(Path, 0);
IRasterWorkspace pRasterWorkspace = pWorkspace as IRasterWorkspace;
IRasterDataset pRasterDataset = pRasterWorkspace.OpenRasterDataset(Name);//
第三步,是否创建金字塔的判断
DialogResult dr=MessageBox.Show("是否创建金字塔?", "提示",MessageBoxButtons.YesNo, MessageBoxIcon.Question); //注意:这里的参数列表可以更换别的,YesNo可以改成OKCancel确定取消等等。
if (dr==DialogResult.OK) //当点击“是”的时候,创建金字塔
{
//影像金字塔的判断与创建,使用IRasterPyramid3接口
IRasterPyramid3 pRasPyrmid;
pRasPyrmid = pRasterDataset as IRasterPyramid3; //接口转换
if (pRasPyrmid != null)
{
if (!(pRasPyrmid.Present))
{
pRasPyrmid.Create();//创建金字塔
}
}
}
如图:
//当点击否的时候,直接加载到地图。
IRaster pRaster;
pRaster = pRasterDataset.CreateDefaultRaster();//
IRasterLayer pRasterLayer;
pRasterLayer = new RasterLayer(); //此处要更改Carto类库的嵌入类型为false
pRasterLayer.CreateFromRaster(pRaster);//
ILayer pLayer = pRasterLayer as ILayer;
axMapControl1.AddLayer(pLayer, 0);