Geotiff学习总结:
/// <summary>
/// Geotiff,Img图片格式转换
/// </summary>
/// <param name="author">Author</param>
/// <param name="ds">数据集</param>
/// <param name="filename">文件名路径</param>
/// <param name="iOverview">波段</param>
private static void SaveBitmapDirect(Dataset ds, string filename, int iOverview)
{
// 获得数据对象
Band redBand = ds.GetRasterBand(1);
if (redBand.GetRasterColorInterpretation() == ColorInterp.GCI_PaletteIndex)
{
SaveBitmapPaletteDirect(ds, filename, iOverview);
return;
}
if (redBand.GetRasterColorInterpretation() == ColorInterp.GCI_GrayIndex)
{
SaveBitmapGrayDirect(ds, filename, iOverview);
return;
}
if (ds.RasterCount < 3)
{
MessageBox.Show("不能转换!");
System.Environment.Exit(-1);
}
if (iOverview >= 0 && redBand.GetOverviewCount() > iOverview)
redBand = redBand.GetOverview(iOverview);
Band greenBand = ds.GetRasterBand(2);
if (iOverview >= 0 && greenBand.GetOverviewCount() > iOverview)
greenBand = greenBand.GetOverview(iOverview);
Band blueBand = ds.GetRasterBand(3);
if (iOverview >= 0 && blueBand.GetOverviewCount() > iOverview)
blueBand = blueBand.GetOverview(iOverview);
// 得到数据集的宽度和高度
int width = redBand.XSize;
int height = redBand.YSize;
// 创建一个储存GDAL的位图图像
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb);
// 使用光栅阅读方法阅读GDAL的图像数据直接进入这个位图
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
try
{
int stride = bitmapData.Stride;
IntPtr buf = bitmapData.Scan0;
blueBand.ReadRaster(0, 0, width, height, buf, width, height, DataType.GDT_Byte, 4, stride);
greenBand.ReadRaster(0, 0, width, height, new IntPtr(buf.ToInt32() + 1), width, height, DataType.GDT_Byte, 4, stride);
redBand.ReadRaster(0, 0, width, height, new IntPtr(buf.ToInt32() + 2), width, height, DataType.GDT_Byte, 4, stride);
}
finally
{
bitmap.UnlockBits(bitmapData);
}
bitmap.Save(filename);
}
/// <summary>
/// 黑白图像格式转换其他格式
/// </summary>
/// <param name="author">聂文礼</param>
/// <param name="ds">数据集</param>
/// <param name="filename">文件名路径</param>
/// <param name="iOverview">波段</param>
private static void SaveBitmapGrayDirect(Dataset ds, string filename, int iOverview)
{
// 得到数据对象 2010-06-03
Band band = ds.GetRasterBand(1);
if (iOverview >= 0 && band.GetOverviewCount() > iOverview)
band = band.GetOverview(iOverview);
// 得到数据集的宽度和高度的
int width = band.XSize;
int height = band.YSize;
// 创建一个储存GDAL的位图图像
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
byte[] r = new byte[width * height];
band.ReadRaster(0, 0, width, height, r, width, height, 0, 0);
// 使用光栅阅读方法阅读GDAL的图像数据直接进入这个位图
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
try
{
ColorPalette pal = bitmap.Palette;
for (int i = 0; i < 256; i++)
pal.Entries[i] = Color.FromArgb(255, i, i, i);
bitmap.Palette = pal;
int stride = bitmapData.Stride;
IntPtr buf = bitmapData.Scan0;
band.ReadRaster(0, 0, width, height, buf, width, height, DataType.GDT_Byte, 1, stride);
}
finally
{
bitmap.UnlockBits(bitmapData);
}
bitmap.Save(filename);
}
/// <summary>
/// RGB调色板类型转换
/// </summary>
/// <param name="author">聂文礼</param>
/// <param name="ds">数据集</param>
/// <param name="filename">文件名路径</param>
/// <param name="iOverview">波段</param>
private static void SaveBitmapPaletteDirect(Dataset ds, string filename, int iOverview)
{
// 获取数据对象
Band band = ds.GetRasterBand(1);
if (iOverview >= 0 && band.GetOverviewCount() > iOverview)
band = band.GetOverview(iOverview);
ColorTable ct = band.GetRasterColorTable();
if (ct == null)
{
MessageBox.Show("波段没有颜色表!");
return;
}
if (ct.GetPaletteInterpretation() != PaletteInterp.GPI_RGB)
{
MessageBox.Show("只支持RGB调色板Interp");
return;
}
// 获取数据集得高度和宽度
int width = band.XSize;
int height = band.YSize;
// 创建一个储存GDAL的位图图像
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
byte[] r = new byte[width * height];
band.ReadRaster(0, 0, width, height, r, width, height, 0, 0);
// 使用光栅阅读方法阅读GDAL的图像数据直接进入这个位图
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
try
{
int iCol = ct.GetCount();
ColorPalette pal = bitmap.Palette;
for (int i = 0; i < iCol; i++)
{
ColorEntry ce = ct.GetColorEntry(i);
pal.Entries[i] = Color.FromArgb(ce.c4, ce.c1, ce.c2, ce.c3);
}
bitmap.Palette = pal;
int stride = bitmapData.Stride;
IntPtr buf = bitmapData.Scan0;
band.ReadRaster(0, 0, width, height, buf, width, height, DataType.GDT_Byte, 1, stride);
}
finally
{
bitmap.UnlockBits(bitmapData);
}
bitmap.Save(filename);
}