首页 > 代码库 > winform对图片进行灰度处理

winform对图片进行灰度处理

        //图片进行灰度处理        //originalImage为原图像   返回灰度图像        private Bitmap GrayImage(Bitmap originalImage)        {            ImageAttributes imageAttributes = new ImageAttributes();            float[][] matrix = { new float[] {0.299f, 0.299f, 0.299f, 0, 0},                                             new float[]   {0.587f,   0.587f,   0.587f,   0,   0},                                              new float[]   {0.114f,   0.114f,   0.114f,   0,   0},                                             new float[]   {0,   0,   0,   1,   0},                                                                new float[]   {0,   0,   0,   0,   1}                                   };            ColorMatrix colorMatrix = new ColorMatrix(matrix);            imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);            Bitmap grayHead = new Bitmap(originalImage.Width, originalImage.Height);            using (Graphics g = Graphics.FromImage(grayHead))            {                g.DrawImage(originalImage, new Rectangle(0, 0, originalImage.Width, originalImage.Height), 0, 0, originalImage.Width, originalImage.Height, GraphicsUnit.Pixel, imageAttributes);            }            return grayHead;        }

效果:

      

winform对图片进行灰度处理