您的位置:首页 > 其它

反转黑白TIFF图像

2009-08-26 16:15 190 查看
将单色(黑白)TIFF图像进行颜色反转处理,即将黑色处理成白色,白色处理成黑色。参考了网上使用指针处理Bitmap的方法!

void ReverseTIFF(string sourceFilName, string destFileName)
{
Bitmap sourBitmap = (Bitmap)Image.FromFile(sourceFilName);
Bitmap destBitmap = new Bitmap(sourBitmap.Width, sourBitmap.Height, PixelFormat.Format1bppIndexed);

BitmapData sourData = sourBitmap.LockBits(new Rectangle(0, 0, sourBitmap.Width, sourBitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
BitmapData destData = destBitmap.LockBits(new Rectangle(0, 0, sourBitmap.Width, sourBitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);

unsafe
{
for (int y = 0; y < sourData.Height; y++)
{
byte* dataIn = (byte*)sourData.Scan0 + (y * sourData.Stride);
byte* dataOut = (byte*)destData.Scan0 + (y * destData.Stride);

for (int x = 0; x < sourData.Width / 8; x++)
{
dataOut[x] = (byte)(~dataIn[x]);
}
}
}

sourBitmap.UnlockBits(sourData);
destBitmap.UnlockBits(destData);
destBitmap.Save(destFileName);

sourBitmap.Dispose();
destBitmap.Dispose();

return;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: