/// <summary> /// 圖像對比度調整 /// </summary> /// <param name="b">原始圖</param> /// <param name="degree">對比度[-100, 100]</param> /// <returns></returns> public static Bitmap KiContrast(Bitmap b, int degree) { if (b == null) { return null; } if (degree < -100) degree = -100; if (degree > 100) degree = 100; try { double pixel = 0; double contrast = (100.0 + degree) / 100.0; contrast *= contrast; int width = b.Width; int height = b.Height; 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++) { pixel = ((p[i] / 255.0 - 0.5) * contrast + 0.5) * 255; if (pixel < 0) pixel = 0; if (pixel > 255) pixel = 255; p[i] = (byte)pixel; } // i p += 3; } // x p += offset; } // y } b.UnlockBits(data); return b; } catch { return null; } } // end of Contrast
1 /// <summary> 2 /// 亮度 3 /// </summary> 4 /// <param name="a"></param> 5 /// <param name="v"></param> 6 /// <returns></returns> 7 private static Bitmap BrightnessP(Bitmap a, int v) 8 { 9 System.Drawing.Imaging.BitmapData bmpData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb); 10 int bytes = a.Width * a.Height * 3; 11 IntPtr ptr = bmpData.Scan0; 12 int stride = bmpData.Stride; 13 unsafe 14 { 15 byte* p = (byte*)ptr; 16 int temp; 17 for (int j = 0; j < a.Height; j++) 18 { 19 for (int i = 0; i < a.Width * 3; i++, p++) 20 { 21 temp = (int)(p[0] + v); 22 temp = (temp > 255) ? 255 : temp < 0 ? 0 : temp; 23 p[0] = (byte)temp; 24 } 25 p += stride - a.Width * 3; 26 } 27 } 28 a.UnlockBits(bmpData); 29 return a; 30 }