/// <summary> /// 灰階 /// </summary> /// <param name="bmp"></param> /// <param name="type"></param> /// <returns></returns> public static Bitmap GrayImage(Bitmap bmp, GrayAlgorithmTypes type) { int height = bmp.Height; int width = bmp.Width; Bitmap newbmp = new Bitmap(width, height); LockBitmap lbmp = new LockBitmap(bmp); LockBitmap newlbmp = new LockBitmap(newbmp); lbmp.LockBits(); newlbmp.LockBits(); Color pixel; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { pixel = lbmp.GetPixel(x, y); int r, g, b, Result = 0; r = pixel.R; g = pixel.G; b = pixel.B; switch (type) { case GrayAlgorithmTypes.Average: //平均值 Result = ((r + g + b) / 3); break; case GrayAlgorithmTypes.Maximum: //最大值 Result = r > g ? r : g; Result = Result > b ? Result : b; break; case GrayAlgorithmTypes.WeightedAverage: //加權平均值 Result = ((int)(0.299 * r) + (int)(0.587 * g) + (int)(0.114 * b)); break; } newlbmp.SetPixel(x, y, Color.FromArgb(Result, Result, Result)); } } lbmp.UnlockBits(); newlbmp.UnlockBits(); return(newbmp); }
public GrayFilter(GrayAlgorithmTypes type) { Type = type; }
public GrayFilter() { Type = GrayAlgorithmTypes.Average; }