C# (灰度)加权平均法将图片转换为灰度图

        private Bitmap ToG(string file)
        {
            using (Bitmap o = new Bitmap(file))
            {
                Bitmap g = new Bitmap(o.Width, o.Height);
                for (int i = 0; i < o.Width; i++)
                {
                    for (int j = 0; j < o.Height; j++)
                    {
                        Color c = o.GetPixel(i, j);
                        //灰度加权平均法公式
                            int rgb = (int)(c.R * 0.299 + c.G * 0.587 + c.B * 0.114);
                        g.SetPixel(i, j, Color.FromArgb(rgb, rgb, rgb));
                    }
                }
                return g;
            }
        }
原文地址:https://www.cnblogs.com/wjshan0808/p/5941323.html