Geotiff图像解析

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);
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值