[已解决] 关于图框裁剪
有若干不同图幅的JPG格式影像,为接边方便有重叠,现需要吧重复的外围部分裁剪掉,如何处理,谁做过的能不能给点代码或思路提示,谢过。 |
我参考 lsbocai 2008-5-28 影像数据裁切和保存 一文 但是在保存的时候 不管是直接保存成图像 还是保存到数据库 都有问题 请高手指点 //影像裁切 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); |
我又换一种方法来裁剪,通过四角坐标创建多边形来裁剪,可以裁剪也可以保存但是过程中会出现错误提示 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
整个代码如下 , 麻烦指点下
private void clipToolStripMenuItem_Click(object sender, EventArgs e)
{
BeginClip();
}
public IRaster ClipRasterByPolygon(IGeoDataset pInGeoDataset, IPolygon pPolygon)
{
IRaster pRaster =null;
if (pInGeoDataset is IRasterLayer)
{
IRasterLayer rasterLayer = pInGeoDataset as IRasterLayer;
pRaster = rasterLayer.Raster;
}
else if (pInGeoDataset is IRasterDataset)
{
IRasterDataset rasterDataset = pInGeoDataset as IRasterDataset;
pRaster = rasterDataset.CreateDefaultRaster();
}
else if (pInGeoDataset is IRaster)
{
pRaster = pInGeoDataset as IRaster;
}
else
{
return null;
}
IGeoDataset pInputDataset = pRaster as IGeoDataset;
IExtractionOp pExtractionOp = new RasterExtractionOpClass();
IRasterAnalysisEnvironment pRasterAnalysisEnvironment = pExtractionOp as IRasterAnalysisEnvironment;
object cellSizeProvider = GetRasterCellSize(pRaster);
pRasterAnalysisEnvironment.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSizeProvider);
object extentProvider = pPolygon.Envelope;
object snapRasterData = Type.Missing;
pRasterAnalysisEnvironment.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref extentProvider, ref snapRasterData);
IGeoDataset pOutputDataset = pExtractionOp.Polygon(pInputDataset, pPolygon, true);
IRaster clipRaster = pOutputDataset as IRaster;
return clipRaster;
}
public double GetRasterCellSize(IRaster pRaster)//获取RasterCellSize
{
IRasterProps pProps = pRaster as IRasterProps;
return pProps.MeanCellSize().X;
}
public void BeginClip()
{
try
{
ILayer layer = axMapControl1.get_Layer(0); //要裁切的影像图层
IRasterLayer pRasterLayer = layer as IRasterLayer;
//IRaster pRaster = pRasterLayer.Raster;
IGeoDataset pGeoDataSet;
pGeoDataSet = layer as IGeoDataset;
double StartX, StartY, dx, dy, Xmin, Ymin, Dx, Dy;
//dx = 600; dy = 500; Dx = 3922; Dy = 2680;
dx = 50; dy = 50; Dx = 600; Dy = 600;
Xmin = pRasterLayer.VisibleExtent.XMin;
Ymin = pRasterLayer.VisibleExtent.YMin;
StartX = Xmin + dx;
StartY = Ymin + dy;
IRaster pRaster;
IPolygon pPolygon = CreatePolygon(StartX, StartY, Dx, Dy);
pRaster = ClipRasterByPolygon(pGeoDataSet, pPolygon);
//pRasterLayer.CreateFromRaster(pRaster);
//axMapControl1.AddLayer(pRasterLayer);
//保存影像文件
IWorkspaceFactory pWKSF = new RasterWorkspaceFactoryClass();//
IWorkspace pWorkspace = pWKSF.OpenFromFile(@"E:\\work\\raster", 0);
ISaveAs pSaveAs = pRaster as ISaveAs;
pSaveAs.SaveAs("test02.jpg", pWorkspace, "JPG");
pRasterLayer.CreateFromRaster(pRaster);
axMapControl1.AddLayer(pRasterLayer);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
////保存到mdb
//IRasterStorageDef pRasterStorageDef = new RasterStorageDefClass();
//pRasterStorageDef.CompressionType = esriRasterCompressionType.esriRasterCompressionJPEG2000;
//pRasterStorageDef.CompressionQuality = 50;
//pRasterStorageDef.TileHeight = 128;
//pRasterStorageDef.TileWidth = 128;
//IWorkspaceFactory pWKSF = new ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactoryClass();
//IWorkspace pWorkspace = pWKSF.OpenFromFile(@"e:\work\db2.mdb", 0);
////IWorkspace pWorkspace = (IWorkspace)OpenFileGDBWorkspace(@"e:\work\db2.mdb");
//ISaveAs2 pSaveAs = clipRaster as ISaveAs2;
//pSaveAs.SaveAsRasterDataset("compressedRaster", pWorkspace, "GDB", pRasterStorageDef);
}
public IPolygon CreatePolygon(double StartX, double StartY, double dx, double dy)
{
IPointCollection4 pPntsCol;
IPoint pLeftBottomPoint, pLeftTopPoint, pRightTopPoint, pRightBottomPoint;
pPntsCol = new PolygonClass();
pLeftBottomPoint = new PointClass();
pLeftBottomPoint.X = StartX;
pLeftBottomPoint.Y = StartY;
object missing = Type.Missing;
pPntsCol.AddPoint(pLeftBottomPoint, ref missing, ref missing);
pLeftTopPoint = new PointClass();
pLeftTopPoint.X = StartX;
pLeftTopPoint.Y = StartY + dy;
pPntsCol.AddPoint(pLeftTopPoint, ref missing, ref missing);
pRightTopPoint = new PointClass();
pRightTopPoint.X = StartX + dx;
pRightTopPoint.Y = StartY + dy;
pPntsCol.AddPoint(pRightTopPoint, ref missing, ref missing);
pRightBottomPoint = new PointClass();
pRightBottomPoint.X = StartX + dx;
pRightBottomPoint.Y = StartY;
pPntsCol.AddPoint(pRightBottomPoint, ref missing, ref missing);
IPolygon4 pPoly = pPntsCol as IPolygon4;
pPoly.Close();
IMap pMap = axMapControl1.Map;
ISpatialReference pSRMap = pMap.SpatialReference;// as ISpatialReference;
pPoly.SpatialReference = pSRMap;
IPolygon pReturnPolygon = pPoly as IPolygon;
return pReturnPolygon;
}
整个代码如下 , 麻烦指点下
private void clipToolStripMenuItem_Click(object sender, EventArgs e)
{
BeginClip();
}
public IRaster ClipRasterByPolygon(IGeoDataset pInGeoDataset, IPolygon pPolygon)
{
IRaster pRaster =null;
if (pInGeoDataset is IRasterLayer)
{
IRasterLayer rasterLayer = pInGeoDataset as IRasterLayer;
pRaster = rasterLayer.Raster;
}
else if (pInGeoDataset is IRasterDataset)
{
IRasterDataset rasterDataset = pInGeoDataset as IRasterDataset;
pRaster = rasterDataset.CreateDefaultRaster();
}
else if (pInGeoDataset is IRaster)
{
pRaster = pInGeoDataset as IRaster;
}
else
{
return null;
}
IGeoDataset pInputDataset = pRaster as IGeoDataset;
IExtractionOp pExtractionOp = new RasterExtractionOpClass();
IRasterAnalysisEnvironment pRasterAnalysisEnvironment = pExtractionOp as IRasterAnalysisEnvironment;
object cellSizeProvider = GetRasterCellSize(pRaster);
pRasterAnalysisEnvironment.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSizeProvider);
object extentProvider = pPolygon.Envelope;
object snapRasterData = Type.Missing;
pRasterAnalysisEnvironment.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref extentProvider, ref snapRasterData);
IGeoDataset pOutputDataset = pExtractionOp.Polygon(pInputDataset, pPolygon, true);
IRaster clipRaster = pOutputDataset as IRaster;
return clipRaster;
}
public double GetRasterCellSize(IRaster pRaster)//获取RasterCellSize
{
IRasterProps pProps = pRaster as IRasterProps;
return pProps.MeanCellSize().X;
}
public void BeginClip()
{
try
{
ILayer layer = axMapControl1.get_Layer(0); //要裁切的影像图层
IRasterLayer pRasterLayer = layer as IRasterLayer;
//IRaster pRaster = pRasterLayer.Raster;
IGeoDataset pGeoDataSet;
pGeoDataSet = layer as IGeoDataset;
double StartX, StartY, dx, dy, Xmin, Ymin, Dx, Dy;
//dx = 600; dy = 500; Dx = 3922; Dy = 2680;
dx = 50; dy = 50; Dx = 600; Dy = 600;
Xmin = pRasterLayer.VisibleExtent.XMin;
Ymin = pRasterLayer.VisibleExtent.YMin;
StartX = Xmin + dx;
StartY = Ymin + dy;
IRaster pRaster;
IPolygon pPolygon = CreatePolygon(StartX, StartY, Dx, Dy);
pRaster = ClipRasterByPolygon(pGeoDataSet, pPolygon);
//pRasterLayer.CreateFromRaster(pRaster);
//axMapControl1.AddLayer(pRasterLayer);
//保存影像文件
IWorkspaceFactory pWKSF = new RasterWorkspaceFactoryClass();//
IWorkspace pWorkspace = pWKSF.OpenFromFile(@"E:\\work\\raster", 0);
ISaveAs pSaveAs = pRaster as ISaveAs;
pSaveAs.SaveAs("test02.jpg", pWorkspace, "JPG");
pRasterLayer.CreateFromRaster(pRaster);
axMapControl1.AddLayer(pRasterLayer);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
////保存到mdb
//IRasterStorageDef pRasterStorageDef = new RasterStorageDefClass();
//pRasterStorageDef.CompressionType = esriRasterCompressionType.esriRasterCompressionJPEG2000;
//pRasterStorageDef.CompressionQuality = 50;
//pRasterStorageDef.TileHeight = 128;
//pRasterStorageDef.TileWidth = 128;
//IWorkspaceFactory pWKSF = new ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactoryClass();
//IWorkspace pWorkspace = pWKSF.OpenFromFile(@"e:\work\db2.mdb", 0);
////IWorkspace pWorkspace = (IWorkspace)OpenFileGDBWorkspace(@"e:\work\db2.mdb");
//ISaveAs2 pSaveAs = clipRaster as ISaveAs2;
//pSaveAs.SaveAsRasterDataset("compressedRaster", pWorkspace, "GDB", pRasterStorageDef);
}
public IPolygon CreatePolygon(double StartX, double StartY, double dx, double dy)
{
IPointCollection4 pPntsCol;
IPoint pLeftBottomPoint, pLeftTopPoint, pRightTopPoint, pRightBottomPoint;
pPntsCol = new PolygonClass();
pLeftBottomPoint = new PointClass();
pLeftBottomPoint.X = StartX;
pLeftBottomPoint.Y = StartY;
object missing = Type.Missing;
pPntsCol.AddPoint(pLeftBottomPoint, ref missing, ref missing);
pLeftTopPoint = new PointClass();
pLeftTopPoint.X = StartX;
pLeftTopPoint.Y = StartY + dy;
pPntsCol.AddPoint(pLeftTopPoint, ref missing, ref missing);
pRightTopPoint = new PointClass();
pRightTopPoint.X = StartX + dx;
pRightTopPoint.Y = StartY + dy;
pPntsCol.AddPoint(pRightTopPoint, ref missing, ref missing);
pRightBottomPoint = new PointClass();
pRightBottomPoint.X = StartX + dx;
pRightBottomPoint.Y = StartY;
pPntsCol.AddPoint(pRightBottomPoint, ref missing, ref missing);
IPolygon4 pPoly = pPntsCol as IPolygon4;
pPoly.Close();
IMap pMap = axMapControl1.Map;
ISpatialReference pSRMap = pMap.SpatialReference;// as ISpatialReference;
pPoly.SpatialReference = pSRMap;
IPolygon pReturnPolygon = pPoly as IPolygon;
return pReturnPolygon;
}