C#调节图片亮度
昨天去客户那里测试,需求才开始,所以很简单,就是测一下能不能接受到视频或图片,然后保存下来,现场客户说亮度不够,然后学了一下C#调节图片亮度,记录一下
学习的帖子在这:http://blog.csdn.net/kenkao/article/details/3148091
/// <summary> /// 图像明暗调整 /// </summary> /// <param name="b">原始图</param> /// <param name="degree">亮度[-255, 255]</param> /// <returns></returns> public static Bitmap KiLighten(Bitmap b, int degree) { if (b == null) { return null; } if (degree < -255) degree = -255; if (degree > 255) degree = 255; try { int width = b.Width; int height = b.Height; int pix = 0; BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); unsafe { byte* p = (byte*)data.Scan0; int offset = data.Stride - width * 3; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // 处理指定位置像素的亮度 for (int i = 0; i < 3; i++) { pix = p[i] + degree; if (degree < 0) p[i] = (byte)Math.Max(0, pix); if (degree > 0) p[i] = (byte)Math.Min(255, pix); } // i p += 3; } // x p += offset; } // y } b.UnlockBits(data); return b; } catch { return null; } } // end of Lighten
然后调用
string savePath = @"C:\Users\wjr\Desktop\出差用\1.png"; Bitmap img = new Bitmap(@"C:\Users\wjr\Desktop\出差用\WindowsFormsApplication1\WindowsFormsApplication1\WindowsFormsApplication1\bin\Release\Capture\20170417 025826019.png"); Bitmap a = KiLighten(img, 20); a.Save(savePath);
Bitmap a = KiLighten(img, 20);
我这里设置的是20,范围是-255~255,所以0是原图片亮度,我设置的20,所以图片亮度想过显示不明显,我弄个100的,看下效果