using OSGeo.GDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace antiColor
{
class Program
{
static void Main(string[] args)
{
Gdal.AllRegister();
string srcFile = @"E:\GDAL\GDALPractice\GDAL_in_CSharp\chapter07\ZoomFile01.tif";
Dataset srcDs= Gdal.Open(srcFile, Access.GA_ReadOnly);
Driver srcDrv = srcDs.GetDriver();
int xSize = srcDs.RasterXSize;
int ySize = srcDs.RasterYSize;
int bandCount = srcDs.RasterCount;
string dstFile=@"E:\GDAL\GDALPractice\GDAL_in_CSharp\chapter07\antiColor.tif";
Dataset dstDs= srcDrv.Create(dstFile, xSize, ySize, bandCount, DataType.GDT_Byte, null);
byte[] srcArray=new byte[xSize];//存储一行的大小
byte[] dstArray=new byte[xSize];
for (int i = 1; i <= bandCount; i++)
{
Band srcBand = srcDs.GetRasterBand(i);
Band dstBand = dstDs.GetRasterBand(i);
for (int j = 0; j < ySize; j++)//循环图像高
{
srcBand.ReadRaster(0, j, xSize, 1, srcArray, xSize, 1, 0, 0);
for (int h = 0; h < xSize; h++)//循环图像宽
{
dstArray[h] = (byte)((255) - srcArray[h]);
}
dstBand.WriteRaster(0, j, xSize, 1, dstArray, xSize, 1, 0, 0);
}
}
dstDs.FlushCache();
dstDs.Dispose();
Console.WriteLine("成功");
Console.ReadKey();
}
}
}
效果:
原图:
反色结果: