影像数据的裁切和保存(源代码)
因为问题已经解决,现将代码整理给大家。 影像数据的裁切我是参考的scrsgjh的帖子(vb的代码),原帖地址http://bbs.esrichina-bj.cn/ESRI/viewthread.php? tid=4152&highlight=%B6%E0%B1%DF%D0%CE%2Bscrsgjh //影像裁切 IGeometry clipGeo = axMapControl1.TrackPolygon(); // ILayer layer = axMapControl1.get_Layer(i); //要裁切的影像图层 IRasterLayer pRasterLayer = layer as IRasterLayer; 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(@"C:\temp", 0); ISaveAs pSaveAs = clipRaster as ISaveAs; pSaveAs.SaveAs("test.img", pWorkspace, "IMAGINE Image"); //如果保存在mdb中 IRasterStorageDef pRasterStorageDef = new RasterStorageDefClass(); pRasterStorageDef.CompressionType = esriRasterCompressionType.esriRasterCompressionJPEG2000; pRasterStorageDef.CompressionQuality = 50; pRasterStorageDef.TileHeight = 128; pRasterStorageDef.TileWidth = 128; IWorkspaceFactory pWKSF = new AccessWorkspaceFactoryClass(); IWorkspace pWorkspace = pWKSF.OpenFromFile(@"C:\temp\test.mdb", 0); ISaveAs2 pSaveAs = clipRaster as ISaveAs2; pSaveAs.SaveAsRasterDataset("test", pWorkspace, "gdb", pRasterStorageDef); //也可以使用IRasterWorkspaceEx的SaveAsRasterDataset方法保存到mdb中 IWorkspaceFactory pWKSF = new AccessWorkspaceFactoryClass(); IWorkspace pWorkspace = pWKSF.OpenFromFile(@"C:\temp\test.mdb", 0); IRasterWorkspaceEx pRasterWKS = pWorkspace as IRasterWorkspaceEx; IRasterStorageDef pRasterStorageDef = new RasterStorageDefClass(); pRasterStorageDef.CompressionType = esriRasterCompressionType.esriRasterCompressionJPEG2000; pRasterStorageDef.CompressionQuality = 50; pRasterStorageDef.PyramidLevel = 2; pRasterStorageDef.PyramidResampleType = rstResamplingTypes.RSP_BilinearInterpolation; pRasterStorageDef.TileHeight = 128; pRasterStorageDef.TileWidth = 128; IRasterDef pRasterDef = new RasterDefClass(); pRasterDef.Description = "rasterdataset"; pRasterDef.SpatialReference = axMapControl1.SpatialReference; IGeometryDef pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = pGeoDef as IGeometryDefEdit; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; pGeoDefEdit.AvgNumPoints_2 = 4; pGeoDefEdit.GridCount_2 = 1; pGeoDefEdit.set_GridSize(0, 1000); pGeoDefEdit.SpatialReference_2 = axMapControl1.SpatialReference; IRasterDataset pRasterDataset = pRasterWKS.SaveAsRasterDataset("test", clipRaster, pRasterStorageDef, "", pRasterDef, pGeoDef); //其实参数可以不用设置,直接: IRasterDataset pRasterDataset = pRasterWKS.SaveAsRasterDataset("test", clipRaster, null, "", null, null); |