暗调滤镜

最后更新于:2022-04-01 06:45:55

本文介绍暗调滤镜的实现过程,这个滤镜主要是呈现一种暗调,对比度明显的效果,原理很简单,公式如下: newR = R*R/255; newG = G*G/255; newB = B*B/255; 实现代码如下: ~~~ private Bitmap FilterProcess(Bitmap a) { Bitmap srcBitmap = new Bitmap(a); int w = srcBitmap.Width; int h = srcBitmap.Height; System.Drawing.Imaging.BitmapData srcData = srcBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb); byte* pSrc = (byte*)srcData.Scan0; int offset = srcData.Stride - w * 4; int r, g, b; for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { b = pSrc[0]; g = pSrc[1]; r = pSrc[2]; pSrc[2] = (byte)(r * r / 255); pSrc[1] = (byte)(g * g / 255); pSrc[0] = (byte)(b * b / 255); pSrc += 4; } pSrc += offset; } srcBitmap.UnlockBits(srcData); return srcBitmap; } ~~~ 效果如下: ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-05_568b3321563f6.png) 原图 [![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-05_568b33218f01f.png)](http://www.zealpixel.com/data/attachment/portal/201508/16/104643w1swwr11iz7fdf8r.png) 暗调效果图 最后,放上一个完整的C#/C程序DEMO下载链接:[http://www.zealpixel.com/forum.php?mod=viewthread&tid=79&extra=page%3D1](http://www.zealpixel.com/forum.php?mod=viewthread&tid=79&extra=page%3D1)
';