GDAL配置
Gdal.AllRegister();
//引用
using System;
using System.Drawing;
using System.Windows.Forms;
using OSGeo.GDAL;
initial();
//文件路径
string fileName = "";
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "所有文件|*.*|Tiff文件|*.tif|Erdas img文件|*.img|Bmp文件|*.bmp|jpeg文件|*.jpg";
if (openFile.ShowDialog() == DialogResult.OK)
{
fileName = openFile.FileName;
}
else
{
MessageBox.Show("影像路径不能为空","注意",
MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
return;
}
textBox1.Text = fileName;
//创建Dataset
Gdal.AllRegister();
Dataset ds = null;
try
{
ds = Gdal.Open(fileName, Access.GA_ReadOnly);
}
catch (Exception ex)
{
MessageBox.Show("打开影像失败"+ex.Message);
}
if (ds == null)
{
MessageBox.Show("影像无效");
return;
}
//创建包络矩形
Rectangle rec = new Rectangle();
rec.Width = this.pictureBox1.Width;
rec.Height = this.pictureBox1.Height;
//调用ReadImage方法获取图像并显示
Bitmap map = ReadImage(ds, rec);
pictureBox1.Image = map;
#region 获取图像信息
int imageWidth = ds.RasterXSize;
int imageHeight = ds.RasterYSize;
float imgRatio = imageWidth / (float)imageHeight;
//图像颜色模式
int model;
model = ds.RasterCount;
double[] dd = new double[4];
ds.GetGeoTransform(dd);
string prj = ds.GetProjection();
string str = string.Format("波段数目:{0}\n行数:{1};列数:{2}\n坐标参考:{3},{4},{5},{6}\n", ds.RasterCount, ds.RasterXSize, ds.RasterYSize, dd[0], dd[1], dd[2], dd[3]);
str += prj + "\n";
for (int i = 1; i <= ds.RasterCount; ++i)
{
OSGeo.GDAL.Band band = ds.GetRasterBand(i);
str += "波段" + i + ":" + band.DataType.ToString();
}
infoText.Text = str;
#endregion
#region 对图像进行缩放
//依据矩形尺寸设置buffer的尺寸
int boxWidth = rec.Width;
int boxHeight = rec.Height;
float boxRatio = boxWidth / (float)boxHeight;
int bufferHeight, bufferWidth;
if (imgRatio >= boxRatio)
{
bufferWidth = boxWidth;
bufferHeight = (int)(boxWidth / imgRatio);
}
else
{
bufferHeight = boxHeight;
bufferWidth = (int)(boxHeight * imgRatio);
}
#endregion
Bitmap map = new Bitmap(bufferWidth, bufferHeight,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
#region 读取图像波段数据
//如果图像颜色模式为灰度
if (model == 1)
{
Band band1 = ds.GetRasterBand(1);
double[] maxmin1 = { 0, 0 };
band1.ComputeRasterMinMax(maxmin1, 0);
int[] info = new int[bufferHeight * bufferWidth];
band1.ReadRaster(0, 0, imageWidth,
imageHeight, info, bufferWidth, bufferHeight, 0, 0);
int i, j;
for (i = 0; i < bufferHeight; i++)
for (j = 0; j < bufferWidth; j++)
{
int val = info[i * bufferWidth + j];
val = (int)(((val - maxmin1[0]) / (maxmin1[1] - maxmin1[0])) * 255);
Color newColor = Color.FromArgb(val, val, val);
map.SetPixel(j, i, newColor);
}
}
//如果图像颜色模式为彩色
if (model != 1)
{
Band band1 = ds.GetRasterBand(1);
double[] maxmin1 = { 0, 0 };
band1.ComputeRasterMinMax(maxmin1, 0);
int[] r = new int[bufferHeight * bufferWidth];
band1.ReadRaster(0, 0, imageWidth,
imageHeight, r, bufferWidth, bufferHeight, 0, 0);
Band band2 = ds.GetRasterBand(2);
double[] maxmin2 = { 0, 0 };
band2.ComputeRasterMinMax(maxmin2, 0);
int[] g = new int[bufferHeight * bufferWidth];
band2.ReadRaster(0, 0, imageWidth,
imageHeight, g, bufferWidth, bufferHeight, 0, 0);
Band band3 = ds.GetRasterBand(3);
double[] maxmin3 = { 0, 0 };
band3.ComputeRasterMinMax(maxmin3, 0);
int[] b = new int[bufferHeight * bufferWidth];
band3.ReadRaster(0, 0, imageWidth,
imageHeight, b, bufferWidth, bufferHeight, 0, 0);
int i, j;
for (i = 0; i < bufferHeight; i++)
for (j = 0; j < bufferWidth; j++)
{
int rval = r[i * bufferWidth + j];
rval = (int)(((rval - maxmin1[0]) / (maxmin1[1] - maxmin1[0])) * 255);
int gval = g[i * bufferWidth + j];
gval = (int)(((gval - maxmin2[0]) / (maxmin2[1] - maxmin2[0])) * 255);
int bval = b[i * bufferWidth + j];
bval = (int)(((bval - maxmin3[0]) / (maxmin3[1] - maxmin3[0])) * 255);
Color newColor = Color.FromArgb(rval, gval, bval);
map.SetPixel(j, i, newColor);
}
}
return map;
#endregion
int xSize = this.pictureBox1.Width;
int ySize = this.pictureBox1.Height;
Bitmap map = new Bitmap(xSize, ySize, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
for (int i = 0; i < xSize; i++)
for (int j = 0; j < ySize; j++)
{
Random rd = new Random((int)DateTime.Now.Ticks);
Color newColor = Color.FromArgb(rd.Next(0,255), rd.Next(0, 255), rd.Next(0, 255));
map.SetPixel(i, j, newColor);
}
pictureBox1.Image = map;
基于GDAL和C#语言读取栅格影像