源代码下载地址:http://download.csdn.net/detail/ivanljf/5832879
1、首先要配置工程环境,将编译好的9个dll文件放在debug目录下,将带有csharp的四个dll添加到引用;如下图:
2.添加命名空间:using OSGeo.GDAL
3、贴出代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OSGeo.GDAL;//添加该命名空间
namespace Official_BuildOverviews
{
class Program
{
public static void usage()
{
Console.WriteLine("usage: Official_BuildOverviews {GDAL dataet name} {resampling} {level1} {level2} ...");
Console.WriteLine("example: gdaloverviews sample.tif \"NEAREST\" 2 4");
System.Environment.Exit(-1);
}
static void Main(string[] args)
{
if (args.Length <= 2)
usage();
Console.WriteLine("");
try
{
Gdal.AllRegister();
Dataset ds = Gdal.Open(args[0], Access.GA_Update);
if (ds == null)
{
Console.WriteLine("Can't open" + args[0]);
System.Environment.Exit(-1);
}
Console.WriteLine("Raster dataset parameters:");
Console.WriteLine(" Projection:" + ds.GetProjectionRef());
Console.WriteLine(" RasterCount:" + ds.RasterCount);
Console.WriteLine(" RasterSize(" + ds.RasterXSize + "," + ds.RasterYSize + ")");
int[] levels = new int[args.Length - 2];//减去前两个参数,该数组存储级别数(2,4,8,16...)
Console.WriteLine(levels.Length);
for (int i = 2; i < args.Length; i++)
{
levels[i - 2] = int.Parse(args[i]);//从第三个参数开始存储
}
if (ds.BuildOverviews(args[1], levels, new Gdal.GDALProgressFuncDelegate(ProgressFunc), "Sample Data") != (int)CPLErr.CE_None)
{
Console.WriteLine("The BuildOverviews operation doesn't work");
System.Environment.Exit(-1);
}
for (int iBand = 1; iBand <= ds.RasterCount;iBand++ )
{
Band band = ds.GetRasterBand(iBand);
Console.WriteLine("Band " + iBand + ":");
Console.WriteLine(" DataType: " + band.DataType);
Console.WriteLine(" Size (" + band.XSize + "," + band.YSize + ")");
Console.WriteLine(" PaletteInterp: " + band.GetRasterColorInterpretation().ToString());
for (int iOver = 0; iOver < band.GetOverviewCount();iOver++ )
{
Band over = band.GetOverview(iOver);
Console.WriteLine(" OverView " + iOver + ":");
Console.WriteLine(" DataType:" + over.DataType);
Console.WriteLine(" Size (" + over.XSize + "," + over.YSize + ")");
Console.WriteLine(" PaletteInterp: " + over.GetRasterColorInterpretation().ToString());
}
}
Console.WriteLine("Completed.");
Console.WriteLine("Use: gdalread " + args[0] + " outfile.png [overview] to extract a particular overview!");
}
catch (System.Exception ex)
{
Console.WriteLine("Application error: " + ex.Message);
}
}
public static int ProgressFunc(double Complete, IntPtr Message, IntPtr Data)//和委托参数一致 public delegate int GDALProgressFuncDelegate(double Complete, IntPtr Message, IntPtr Data);
{
Console.Write("Processing ..." + Complete * 100 + "% Completed.");
if (Message != IntPtr.Zero)
{
Console.Write("Message:" + System.Runtime.InteropServices.Marshal.PtrToStringAnsi(Message));
}
if (Data != IntPtr.Zero)
{
Console.Write("Data:" + System.Runtime.InteropServices.Marshal.PtrToStringAnsi(Data));
}
Console.WriteLine("");
return 1;
}
}
}
注:该程序代码是命令行参数程序,若想调试该程序,需要将传递的参数提前设置好: