Example #1
0
        /// <summary>
        /// 图片转换灰度
        /// </summary>
        /// <param name="img"></param>
        /// <param name="algo"></param>
        /// <returns></returns>
        public static Image ToGray(Image img, AlgorithmsType algo)
        {
            int width = img.Width;
            int height = img.Height;

            Bitmap bmp = new Bitmap(img);

            //设定实例BitmapData相关信息
            Rectangle rect = new Rectangle(0, 0, width, height);
            ImageLockMode mode = ImageLockMode.ReadWrite;
            PixelFormat format = PixelFormat.Format32bppArgb;

            //锁定bmp到系统内存中
            BitmapData data = bmp.LockBits(rect, mode, format);

            //获取位图中第一个像素数据的地址
            IntPtr ptr = data.Scan0;

            int numBytes = width * height * 4;
            byte[] rgbValues = new byte[numBytes];

            //将bmp数据Copy到申明的数组中
            Marshal.Copy(ptr, rgbValues, 0, numBytes);

            for (int i = 0; i < rgbValues.Length; i += 4)
            {
                int value = 0;
                switch (algo)
                {
                    //最大值法
                    case AlgorithmsType.MaxValue:
                        value = rgbValues[i] > rgbValues[i + 1] ? rgbValues[i] : rgbValues[i + 1];
                        value = value > rgbValues[i + 2] ? value : rgbValues[i + 2];
                        break;
                    //平均值法
                    case AlgorithmsType.AverageValue:
                        value = (int)((rgbValues[i] + rgbValues[i + 1] + rgbValues[i + 2]) / 3);
                        break;
                    //加权平均值法
                    case AlgorithmsType.WeightAverage:
                        value = (int)(rgbValues[i] * 0.1 + rgbValues[i + 1] * 0.2
                        + rgbValues[i + 2] * 0.7);
                        break;
                }

                //将数组中存放R、G、B的值修改为计算后的值
                for (int j = 0; j < 3; j++)
                {
                    rgbValues[i + j] = (byte)value;
                }
            }

            //将数据Copy到内存指针
            Marshal.Copy(rgbValues, 0, ptr, numBytes);

            //从系统内存解锁bmp
            bmp.UnlockBits(data);

            return (Image)bmp;
        }
Example #2
0
        /// <summary>
        /// 灰度
        /// </summary>
        /// <param name="img"></param>
        /// <param name="algo"></param>
        /// <returns></returns>
        public static Image Gray(Image img, AlgorithmsType algo)
        {
            int width  = img.Width;
            int height = img.Height;

            Bitmap bmp = new Bitmap(img);

            //设定实例BitmapData相关信息
            Rectangle     rect   = new Rectangle(0, 0, width, height);
            ImageLockMode mode   = ImageLockMode.ReadWrite;
            PixelFormat   format = PixelFormat.Format32bppArgb;

            //锁定bmp到系统内存中
            BitmapData data = bmp.LockBits(rect, mode, format);

            //获取位图中第一个像素数据的地址
            IntPtr ptr = data.Scan0;

            int numBytes = width * height * 4;

            byte[] rgbValues = new byte[numBytes];

            //将bmp数据Copy到申明的数组中
            Marshal.Copy(ptr, rgbValues, 0, numBytes);

            for (int i = 0; i < rgbValues.Length; i += 4)
            {
                int value = 0;
                switch (algo)
                {
                //最大值法
                case AlgorithmsType.MaxValue:
                    value = rgbValues[i] > rgbValues[i + 1] ? rgbValues[i] : rgbValues[i + 1];
                    value = value > rgbValues[i + 2] ? value : rgbValues[i + 2];
                    break;

                //平均值法
                case AlgorithmsType.AverageValue:
                    value = (int)((rgbValues[i] + rgbValues[i + 1] + rgbValues[i + 2]) / 3);
                    break;

                //加权平均值法
                case AlgorithmsType.WeightAverage:
                    value = (int)(rgbValues[i] * 0.1 + rgbValues[i + 1] * 0.2
                                  + rgbValues[i + 2] * 0.7);
                    break;
                }

                //将数组中存放R、G、B的值修改为计算后的值
                for (int j = 0; j < 3; j++)
                {
                    rgbValues[i + j] = (byte)value;
                }
            }

            //将数据Copy到内存指针
            Marshal.Copy(rgbValues, 0, ptr, numBytes);

            //从系统内存解锁bmp
            bmp.UnlockBits(data);

            return((Image)bmp);
        }