本处用到了Band的ReadRaster()和WriteRaster()方法。
private void btnOzil_Click(object sender, EventArgs e)
{
string openFileName = "";
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
openFileName = ofd.FileName;
}
Gdal.AllRegister();
//jpg格式不支持update
Dataset srcDs = Gdal.Open(openFileName, Access.GA_ReadOnly);
Band srcBand1=srcDs.GetRasterBand(1);
Band srcBand2 = srcDs.GetRasterBand(2);
Band srcBand3 = srcDs.GetRasterBand(3);
DataType srcType = srcDs.GetRasterBand(1).DataType;//byte类型
int bandCount = srcDs.RasterCount;
int srcWidth = srcDs.RasterXSize;
int srcHeight = srcDs.RasterYSize;
Debug.WriteLine("原始影像数据类型是:{0}", srcType);
Debug.WriteLine("原始影像的列数:{0}", srcWidth);
Debug.WriteLine("原始影像的行数:{0}", srcHeight);
int[] bandArray = new int[bandCount];
for (int i = 0; i < bandCount; i++)
{
bandArray[i] = i + 1;
}
int[] dstBandArray = new int[]{1,2,3};
int[] dataArray1 = new int[srcWidth * srcHeight * bandCount];
int[] dataArray2 = new int[srcWidth * srcHeight * bandCount];
int[] dataArray3= new int[srcWidth * srcHeight * bandCount];
srcBand1.ReadRaster(0, 0, srcWidth, srcHeight, dataArray1, srcWidth, srcHeight, 0, 0);
srcBand2.ReadRaster(0, 0, srcWidth, srcHeight, dataArray2, srcWidth, srcHeight, 0, 0);
srcBand3.ReadRaster(0, 0, srcWidth, srcHeight, dataArray3, srcWidth, srcHeight, 0, 0);
//注意,JPG没有实现Create方法来创建
//Dataset dstDs= drv.Create(dstFileName,srcWidth,srcHeight,bandCount,DataType.GDT_Byte,null);
//首先创建一个内存的驱动
string strMemory=@"E:\tempMemory.jpg";
Driver dryMemory = Gdal.GetDriverByName("MEM");
//Dataset dsMemory = dryMemory.Create(strMemory, srcWidth, srcHeight,bandCount, DataType.GDT_Byte, null);
Dataset dsMemory = dryMemory.Create(strMemory, srcWidth, srcHeight, 3, DataType.GDT_Byte, null);
Band memBand1 = dsMemory.GetRasterBand(1);
Band memBand2 = dsMemory.GetRasterBand(2);
Band memBand3 = dsMemory.GetRasterBand(3);
if (srcType == DataType.GDT_Byte)
{
int[] dataArray = new int[srcWidth * srcHeight * bandCount];
srcDs.ReadRaster(0, 0, srcWidth, srcHeight, dataArray, srcWidth, srcHeight, bandCount, bandArray, 0, 0, 0);
/***********水平镜像实现**************/
//输出改变前的第200行
//Debug.WriteLine("改变前");
//for(int ii=0;ii<srcWidth;++ii)
//{
// Debug.Write(dataArray[200*srcWidth+ii].ToString() + " ");
//}
//Debug.WriteLine("改变后");
int temp;
for (int i = 0; i < srcHeight; i++)
{
for (int j = 0; j < srcWidth / 2; j++)
{
temp = dataArray1[i * srcWidth + j];
dataArray1[i * srcWidth + j] = dataArray1[i * srcWidth + (srcWidth - 1 - j)];
dataArray1[i * srcWidth + (srcWidth - 1 - j)] = temp;
}
}
for (int i = 0; i < srcHeight; i++)
{
for (int j = 0; j < srcWidth / 2; j++)
{
temp = dataArray2[i * srcWidth + j];
dataArray2[i * srcWidth + j] = dataArray2[i * srcWidth + (srcWidth - 1 - j)];
dataArray2[i * srcWidth + (srcWidth - 1 - j)] = temp;
}
}
for (int i = 0; i < srcHeight; i++)
{
for (int j = 0; j < srcWidth / 2; j++)
{
temp = dataArray3[i * srcWidth + j];
dataArray3[i * srcWidth + j] = dataArray3[i * srcWidth + (srcWidth - 1 - j)];
dataArray3[i * srcWidth + (srcWidth - 1 - j)] = temp;
}
}
memBand1.WriteRaster(0, 0, srcWidth, srcHeight, dataArray1, srcWidth, srcHeight, 0, 0);
memBand2.WriteRaster(0, 0, srcWidth, srcHeight, dataArray2, srcWidth, srcHeight, 0, 0);
memBand3.WriteRaster(0, 0, srcWidth, srcHeight, dataArray3, srcWidth, srcHeight, 0, 0);
}
Driver drvJPG = Gdal.GetDriverByName("JPEG");
string dstFileName = @"E:\right_result_gyy.jpg";
drvJPG.CreateCopy(dstFileName, dsMemory, 1, null, null, null);
//最后释放资源
srcDs.Dispose();
dsMemory.Dispose();
MessageBox.Show("水平镜像:success");
}
原图像1:
图像1水平镜像效果:
原图像2:
图像2水平镜像效果: