码迷,mamicode.com
首页 > 其他好文 > 详细

伽马曲线调整图片

时间:2015-01-21 17:58:25      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

        private void Form1_Load(object sender, EventArgs e)
        {
            img = pictureBox1.Image = Image.FromFile("8.jpg");
        }

        Image img;
        private void button1_Click(object sender, EventArgs e)
        {
            using (Graphics g = pictureBox2.CreateGraphics())
            {
                g.Clear(pictureBox2.BackColor);
                using (Image tmp = SetGamma(img.Clone() as Image, hsbRed.Value, hsbGreen.Value, hsbBlue.Value))
                    pictureBox2.Image = new Bitmap(tmp);
            }
        }
        /// <summary>
        /// 伽马曲线调整
        /// </summary>
        /// <param name="img"></param>
        /// <param name="red"></param>
        /// <param name="green"></param>
        /// <param name="blue"></param>
        public Image SetGamma(Image img, double red, double green, double blue)
        {
            using (Bitmap tmp = img as Bitmap)
            {
                Color c;
                byte[] rG = CreateGammaArray(red);
                byte[] gG = CreateGammaArray(green);
                byte[] bG = CreateGammaArray(blue);
                for (int i = 0; i < tmp.Width; i++)
                {
                    for (int j = 0; j < tmp.Height; j++)
                    {
                        c = tmp.GetPixel(i, j);
                        tmp.SetPixel(i, j, Color.FromArgb(rG[c.R], gG[c.G], bG[c.B]));
                    }
                }
                rG = gG = bG = null;
                return tmp.Clone() as Bitmap;
            }
        }
        /// <summary>
        /// 获取曲线数组
        /// </summary>
        /// <param name="color">色彩</param>
        /// <returns>数组</returns>
        private byte[] CreateGammaArray(double color)
        {
            byte[] gammaArray = new byte[256];
            for (int i = 1; i < 256; i++)
                gammaArray[i] = (byte)Math.Min(255, (int)((255.0 * Math.Pow(i / 255.0, 1.0 / color)) + 0.5));
            return gammaArray;
        }
        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            label5.Text = string.Format("Red={0},Green={1},Blue={2}", hsbRed.Value, hsbGreen.Value, hsbBlue.Value);
            button1.PerformClick();
        }

        private void hsbGreen_Scroll(object sender, ScrollEventArgs e)
        {
            label5.Text = string.Format("Red={0},Green={1},Blue={2}", hsbRed.Value, hsbGreen.Value, hsbBlue.Value);
            button1.PerformClick();
        }

        private void hsbBlue_Scroll(object sender, ScrollEventArgs e)
        {
            label5.Text = string.Format("Red={0},Green={1},Blue={2}", hsbRed.Value, hsbGreen.Value, hsbBlue.Value);
            button1.PerformClick();
        }

技术分享

伽马曲线调整图片

标签:

原文地址:http://www.cnblogs.com/wjshan0808/p/4239494.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!