需要引入DataSourceRaster命名空间。
具体步骤如下:
①:利用工作控件工厂抽象类定义接口变量,利用工作空间工厂的子类RatserWorkspaceFactory对应的组件类RasterWorkspaceFactoryClass实例化该接口变量。
②:定义并实例化工作空间变量,定义并实例化栅格工作空间变量;
③:定义栅格数据集,并利用所选栅格数据文件进行实例化,,判断该文件是否具有金字塔,若没有,则创建金字塔,以提高大数据量图像的显示效率;
④:定义栅格数据,栅格数据层和数据层,并向MapControl控件中添加数据层数据。
具体代码如下:
System.Windows.Forms.OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "*.bmp|*.bmp|*.jpg|*.jpg|*.tif|*.tif"; ofd.ShowDialog(); string filePath = ofd.FileName; //此两个路径和文件名作为参数: string pathName = System.IO.Path.GetDirectoryName(filePath); string fileName = System.IO.Path.GetFileName(filePath); //定义工作空间工厂并实例化: IWorkspaceFactory pWSF; pWSF = new RasterWorkspaceFactoryClass(); // IWorkspace pWS; pWS = pWSF.OpenFromFile(pathName,0); IRasterWorkspace pRWS; pRWS = pWS as IRasterWorkspace; IRasterDataset pRasterDataset; pRasterDataset = pRWS.OpenRasterDataset(fileName); //影像金字塔的判断与创建 IRasterPyramid pRasPyrmid; pRasPyrmid = pRasterDataset as IRasterPyramid; if(pRasPyrmid!=null) { if(!(pRasPyrmid.Present)) { pRasPyrmid.Create(); } } IRaster pRaster; pRaster = pRasterDataset.CreateDefaultRaster(); IRasterLayer pRasterLayer; pRasterLayer = new RasterLayerClass(); pRasterLayer.CreateFromRaster(pRaster); ILayer pLayer = pRasterLayer as ILayer; axMapControl1.AddLayer(pLayer,0);