直接上代码
public class DemHelper
{
private int width;
private int height;
private Band band;
public void Start()
{
Gdal.AllRegister();
}
public void Read(string path)
{
Dataset ds = Gdal.Open(path, Access.GA_ReadOnly);
band = ds.GetRasterBand(1);
width = ds.RasterXSize;
height = ds.RasterYSize;
}
public Bitmap MakeGrayScale()
{
//新建8位灰度图
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
BitmapData bmpdata = bitmap.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
//获取dem信息
double min, max, no;
int hasvalue;
band.GetMaximum(out max, out hasvalue);
band.GetMinimum(out min, out hasvalue);
band.GetNoDataValue(out no, out hasvalue);
double[] data = new double[width * height];
band.ReadRaster(0, 0, width, height, data, width, height, 0, 0);
//计算图像参数
int offset = bmpdata.Stride - bmpdata.Width; //计算每行未用空间字节数
IntPtr ptr = bmpdata.Scan0; //获取首地址
int scanBytes = bmpdata.Stride * bmpdata.Height; //图像字节数= 扫描字节数 * 高度
byte[] grayValues = new byte[scanBytes]; //为图像数据分配内存
//为图像数据赋值
int posSrc = 0, posScan = 0;
for (int i = 0; i < height; i++) //row
{
for (int j = 0; j < width; j++) //col
{
double item = data[posSrc++];
if (item == no)
{
grayValues[posScan++] = 0; //黑色
}
else
{
grayValues[posScan++] = (byte)((item - min) * 256 / (max - min));
}
}
//跳过图像数据每行未用空间的字节, length = stride - width * bytePerPixel
posScan += offset;
}
Marshal.Copy(grayValues, 0, ptr, scanBytes);
bitmap.UnlockBits(bmpdata);
//修改生成位图的索引表,从伪彩修改为灰度
ColorPalette palette;
//获取一个Format8bppIndex格式图像的Palette对象
using(Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
{
palette = bmp.Palette;
}
for(int i = 0; i < 256; i++)
{
palette.Entries[i] = Color.FromArgb(i, i, i);
}
//修改生成位图的索引表
bitmap.Palette = palette;
return bitmap;
}
}
调用示例:
private void button1_Click(object sender, EventArgs e)
{
DemHelper helper = new DemHelper();
helper.Start();
helper.Read(@"D:\testdata\gewang051.img");
Bitmap bmp = helper.MakeGrayScale();
bmp.Save(@"D:\testdata\test.bmp");
MessageBox.Show("转换完成");
}
部分信息参考了其他的博客http://blog.csdn.net/azkabannull/article/details/7770157,结果的位图如下: