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

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);
    }
}

 

 

posted @ 2019-11-29 14:52  德丽莎·阿波卡利斯  阅读(860)  评论(0编辑  收藏  举报