• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
wjshan0808

Learn from yesterday, Live for today, For a better tomorrow.
 ————wjshan0808

博客园    首页    新随笔    联系   管理    订阅  订阅

改变图片亮度

        private void button1_Click(object sender, EventArgs e)
        {
            UpdatePicBoxEventHandle UpdatePicBox = new UpdatePicBoxEventHandle(SetBrightness);

            Image img = pictureBox1.Image;
            for (int i =-255; i < 256; i++)
            {
                UpdatePicBox.BeginInvoke(img.Clone() as Bitmap, i, new AsyncCallback(UpdatePicBoxCallBack), UpdatePicBox);
            }
        }
        delegate Image UpdatePicBoxEventHandle(Image img, int contrast);
        public void UpdatePicBoxCallBack(IAsyncResult result)
        {
            if (result.IsCompleted)
            {
                UpdatePicBoxEventHandle updatepicbox = result.AsyncState as UpdatePicBoxEventHandle;
                using (Image img = updatepicbox.EndInvoke(result) as Image)
                {
                    //img.Save(Guid.NewGuid().ToString() + ".jpg");
                    pictureBox2.Image = new Bitmap(img);
                }
            }
        }
        /// <summary>
        /// 设置亮度
        /// </summary>
        /// <param name="img">图片</param>
        /// <param name="brightness">-255到+255之间的数值</param>
        /// <returns>改变的图片</returns>   
        public Image SetBrightness(Image img, int brightness)
        {
            using (Bitmap tmp = (Bitmap)img)
            {
                if (brightness < -255) brightness = -255;
                if (brightness > 255) brightness = 255;
                Color c;
                //遍历图像像素
                for (int i = 0; i < tmp.Width; i++)
                {
                    for (int j = 0; j < tmp.Height; j++)
                    {
                        c = tmp.GetPixel(i, j);
                        int R = c.R + brightness;
                        int G = c.G + brightness;
                        int B = c.B + brightness;

                        if (R < 0) R = 1;
                        if (R > 255) R = 255;

                        if (G < 0) G = 1;
                        if (G > 255) G = 255;

                        if (B < 0) B = 1;
                        if (B > 255) B = 255;
                        //重新设置像素颜色
                        tmp.SetPixel(i, j, Color.FromArgb((byte)R, (byte)G, (byte)B));
                    }
                }
                return (Bitmap)tmp.Clone();
            }
        }


posted @ 2015-01-21 16:14  wjshan0808  阅读(173)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3