• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#淡入淡出图片,改变图片透明度

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1. 淡入淡出

/// <summary>
/// 淡入显示图像
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
    try
    {
        Graphics g = this.panel1.CreateGraphics();
        g.Clear(Color.Transparent);
        int width = MyBitmap.Width;
        int height = MyBitmap.Height;
        ImageAttributes attributes = new ImageAttributes();
        ColorMatrix matrix = new ColorMatrix();
        //创建淡入颜色矩阵
        matrix.Matrix00 = (float)0.0;
        matrix.Matrix01 = (float)0.0;
        matrix.Matrix02 = (float)0.0;
        matrix.Matrix03 = (float)0.0;
        matrix.Matrix04 = (float)0.0;
        matrix.Matrix10 = (float)0.0;
        matrix.Matrix11 = (float)0.0;
        matrix.Matrix12 = (float)0.0;
        matrix.Matrix13 = (float)0.0;
        matrix.Matrix14 = (float)0.0;
        matrix.Matrix20 = (float)0.0;
        matrix.Matrix21 = (float)0.0;
        matrix.Matrix22 = (float)0.0;
        matrix.Matrix23 = (float)0.0;
        matrix.Matrix24 = (float)0.0;
        matrix.Matrix30 = (float)0.0;
        matrix.Matrix31 = (float)0.0;
        matrix.Matrix32 = (float)0.0;
        matrix.Matrix33 = (float)0.0;
        matrix.Matrix34 = (float)0.0;
        matrix.Matrix40 = (float)0.0;
        matrix.Matrix41 = (float)0.0;
        matrix.Matrix42 = (float)0.0;
        matrix.Matrix43 = (float)0.0;
        matrix.Matrix44 = (float)0.0;
        matrix.Matrix33 = (float)1.0;
        matrix.Matrix44 = (float)1.0;
        //从0到1进行修改色彩变换矩阵主对角线上的数值
        //使三种基准色的饱和度渐增
        Single count = (float)0.0;
        while (count < 1.0)
        {
            matrix.Matrix00 = count;
            matrix.Matrix11 = count;
            matrix.Matrix22 = count;
            matrix.Matrix33 = count;
            attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            g.DrawImage(MyBitmap, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel, attributes);
            System.Threading.Thread.Sleep(200);
            count = (float)(count + 0.02);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "信息提示");
    }
}

/// <summary>
/// 淡出显示图像
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
    try
    {
        Graphics g = this.panel1.CreateGraphics();
        g.Clear(Color.Gray);
        int width = MyBitmap.Width;
        int height = MyBitmap.Height;
        ImageAttributes attributes = new ImageAttributes();
        ColorMatrix matrix = new ColorMatrix();
        //创建淡出颜色矩阵
        matrix.Matrix00 = (float)0.0;
        matrix.Matrix01 = (float)0.0;
        matrix.Matrix02 = (float)0.0;
        matrix.Matrix03 = (float)0.0;
        matrix.Matrix04 = (float)0.0;
        matrix.Matrix10 = (float)0.0;
        matrix.Matrix11 = (float)0.0;
        matrix.Matrix12 = (float)0.0;
        matrix.Matrix13 = (float)0.0;
        matrix.Matrix14 = (float)0.0;
        matrix.Matrix20 = (float)0.0;
        matrix.Matrix21 = (float)0.0;
        matrix.Matrix22 = (float)0.0;
        matrix.Matrix23 = (float)0.0;
        matrix.Matrix24 = (float)0.0;
        matrix.Matrix30 = (float)0.0;
        matrix.Matrix31 = (float)0.0;
        matrix.Matrix32 = (float)0.0;
        matrix.Matrix33 = (float)0.0;
        matrix.Matrix34 = (float)0.0;
        matrix.Matrix40 = (float)0.0;
        matrix.Matrix41 = (float)0.0;
        matrix.Matrix42 = (float)0.0;
        matrix.Matrix43 = (float)0.0;
        matrix.Matrix44 = (float)0.0;
        matrix.Matrix33 = (float)1.0;
        matrix.Matrix44 = (float)1.0;
        //从1到0进行修改色彩变换矩阵主对角线上的数值
        //依次减少每种色彩分量
        Single count = (float)1.0;
        while (count > 0.0)
        {
            matrix.Matrix00 = (float)count;
            matrix.Matrix11 = (float)count;
            matrix.Matrix22 = (float)count;
            matrix.Matrix33 = (float)count;
            attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            g.DrawImage(MyBitmap, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel, attributes);
            System.Threading.Thread.Sleep(20);
            count = (float)(count - 0.01);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "信息提示");
    }
}

 

2. 透明度

/// <summary>
/// 改变图片的透明度
/// </summary>
/// <param name="image">图片</param>
/// <param name="alpha"></param>
/// <returns></returns>
private void ChangeAlpha(Bitmap img, int alpha)
{
    try
    {
        using (Graphics g = Graphics.FromImage(img))
        {
            g.DrawImage(img, 0, 0);
            for (int h = 0; h <= img.Height - 1; h++)
            {
                for (int w = 0; w <= img.Width - 1; w++)
                {
                    Color c = img.GetPixel(w, h);
                    img.SetPixel(w, h, Color.FromArgb(alpha, c.R, c.G, c.B));
                }
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("ChangeAlpha:" + ex.Message, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap