背景:我有n副不同时间段的太平洋地区的bmp图片,想加载到axmapcontrol中,但是不想一幅幅手动配准生成bpw,工作量太大。
原理:将需要配准的图片放到axmapcontrol中,得到它的初始范围,接着定下配准范围(定死,太平洋),最后源文件下后配准文件了,重新加载图片即可。
代码:
private void Adjust_Click(object sender, EventArgs e)
{
double pX, pY;
IRasterDataset2 rasterDataset = new RasterDatasetClass();
object obj = Type.Missing;
IRasterLayer pRasterLayer = (IRasterLayer)this.axMapControl1.Map.get_Layer(0);//找到bmp图
IRaster pRaster = pRasterLayer.Raster;
rasterDataset.OpenFromFile(pRasterLayer.Name);
IGeoDataset pGeoDataset = (IGeoDataset)pRaster;
IEnvelope2 pEnv2 = new EnvelopeClass();//得到bmp在axmapcontrol中的范围
pEnv2 = (IEnvelope2)pGeoDataset.Extent;
IPoint pPoint = pEnv2.UpperLeft;//bmp左上角点坐标
IPointCollection sourcePoints = new MultipointClass();
IPointCollection targetPoints = new MultipointClass();
IPoint pPoint1 = new PointClass();//定义6个控制点
IPoint pPoint2 = new PointClass();
IPoint pPoint3 = new PointClass();
IPoint pPoint4 = new PointClass();
IPoint pPoint5 = new PointClass();
IPoint pPoint6 = new PointClass();
pPoint1.X = pEnv2.XMin;//图片左上角原始位置
pPoint1.Y = pEnv2.YMax;
pPoint2.X = pEnv2.XMax;//图片右下角原始位置
pPoint2.Y = pEnv2.YMin;
pPoint3.X = 20;//图片左上角配准后位置
pPoint3.Y = 90;
pPoint4.X = 380;//图片右下角配准后位置
pPoint4.Y = -90;
pPoint5.X = pEnv2.XMin;//图片左下角原始位置
pPoint5.Y = pEnv2.YMin;
pPoint6.X = 20;//图片左下角配准后位置
pPoint6.Y = -90;
sourcePoints.AddPoint(pPoint1, ref obj, ref obj);
sourcePoints.AddPoint(pPoint2, ref obj, ref obj);
sourcePoints.AddPoint(pPoint5, ref obj, ref obj);
targetPoints.AddPoint(pPoint3, ref obj, ref obj);
targetPoints.AddPoint(pPoint4, ref obj, ref obj);
targetPoints.AddPoint(pPoint6, ref obj, ref obj);
GeoreferenceRaster(rasterDataset, sourcePoints, targetPoints); //配准
}
/// <summary>
/// 配准
/// </summary>
/// <param name="rasterDataset"></param>
/// <param name="sourcePoints"></param>
/// <param name="targetPoints"></param>
public static void GeoreferenceRaster(IRasterDataset2 rasterDataset, IPointCollection sourcePoints, IPointCollection targetPoints)
{
IRasterGeometryProc rasterPropc = new RasterGeometryProcClass();
IRaster raster = rasterDataset.CreateDefaultRaster(); //set the transformatin
rasterPropc.Warp(sourcePoints, targetPoints, esriGeoTransTypeEnum.esriGeoTransPolyOrder1, raster);//核心,wrap事件
rasterPropc.Register(raster);//注册,生成配准文件处。。此时在源文件目录下
//rasterPropc.Rectify(@"d:\georeferencing_output.img", "IMAGINE Image", raster);//另存为
}
来源:https://blog.csdn.net/guzicheng1990/article/details/99406852