Esempio n. 1
0
        //返回表示深褐色度的矩阵
        public static float[][] GetSepiaMatrix()
        {
            //如果先把图片变黑,深褐色会看上去更好一些,矩阵值(亮度和深褐色化的合并)
            //可以被直接编码,在代码中保留它们为的是更易于修改
            float[][] bright = GetBrightnessMatrix(-25);

            //建立矩阵
            float[][] sepia =
            {
                new float[] { Consts.GrayRed,   Consts.GrayRed,    Consts.GrayRed,   0, 0 },
                new float[] { Consts.GrayGreen, Consts.GrayGreen,  Consts.GrayGreen, 0, 0 },
                new float[] { Consts.GrayBlue,  Consts.GrayBlue,   Consts.GrayBlue,  0, 0 },
                new float[] {                0,                 0,                0, 1, 0 },
                new float[] { Consts.SepiaRed,  Consts.SepiaGreen, Consts.SepiaBlue, 0, 1 }
            };
            return(PhotoHelper.CombineMatrix(bright, sepia));
        }
Esempio n. 2
0
        //  该方法对图片应用所有的颜色操作,以提供一个更加自然的用户体验。
        //  原始的操作顺序被忽略,方法中按照如下顺序对图片进行颜色操作:
        //  1) 灰度转化和褐色转化
        //  2) 对比度、亮度和饱和度调节
        //  3) gamma参数调节
        private void AdjustColor(ref Bitmap image)
        {
            int          brightness = 0;
            int          contrast   = 0;
            int          saturation = 0;
            int          gamma      = 0;
            ConvertColor convert    = ConvertColor.None;

            //  遍历操作列表找到每一个颜色操作的最后设定值
            for (int i = 0; i <= (Global.ActionList.Count - 1); i++)
            {
                ActionItem item = Global.ActionList.GetAt(i);
                switch (item.Action)
                {
                case PhotoAction.Brightness:
                    brightness = item.Percent;
                    break;

                case PhotoAction.Contrast:
                    contrast = item.Percent;
                    break;

                case PhotoAction.Saturation:
                    saturation = item.Percent;
                    break;

                case PhotoAction.Gamma:
                    gamma = item.Percent;
                    break;

                case PhotoAction.ConvertGrayscale:
                    convert = ConvertColor.Grayscale;
                    break;

                case PhotoAction.ConvertSepia:
                    convert = ConvertColor.Sepia;
                    break;
                }
            }
            //  执行颜色操作时更新进度条
            Global.Progress.Update(this, "Applying actions", 1, Global.ActionList.Count);
            //  只有当列表中还有一个或多个操作要执行时才更新
            if (((convert != ConvertColor.None) ||
                 ((contrast != 0) ||
                  ((brightness != 0) ||
                   (saturation != 0)))))
            {
                //  矩阵默认值(单位矩阵)
                float[][] matrix =
                {
                    new float[] { 1, 0, 0, 0, 0 },
                    new float[] { 0, 1, 0, 0, 0 },
                    new float[] { 0, 0, 1, 0, 0 },
                    new float[] { 0, 0, 0, 1, 0 },
                    new float[] { 0, 0, 0, 0, 1 }
                };
                //  灰度转化和褐色转化
                if ((convert == ConvertColor.Grayscale))
                {
                    matrix = PhotoHelper.CombineMatrix(matrix, PhotoHelper.GetGrayscaleMatrix());
                }
                if ((convert == ConvertColor.Sepia))
                {
                    matrix = PhotoHelper.CombineMatrix(matrix, PhotoHelper.GetSepiaMatrix());
                }
                //  对比度、亮度和饱和度调节
                if ((contrast != 0))
                {
                    matrix = PhotoHelper.CombineMatrix(matrix, PhotoHelper.GetContrastMatrix(contrast));
                }
                if ((brightness != 0))
                {
                    matrix = PhotoHelper.CombineMatrix(matrix, PhotoHelper.GetBrightnessMatrix(brightness));
                }
                if ((saturation != 0))
                {
                    matrix = PhotoHelper.CombineMatrix(matrix, PhotoHelper.GetSaturationMatrix(saturation));
                }
                //  对图片执行所有颜色操作
                PhotoHelper.AdjustUsingCustomMatrix(ref image, matrix);
            }
            //  gamma 参数调节,作为最后一个操作来执行
            if ((gamma != 0))
            {
                PhotoHelper.AdjustGamma(ref image, gamma);
            }
        }