首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 圖片動畫顯示技巧(四):淡入效果

     編輯:關於C語言

ColorMatrix 實現淡入

對角線縮放因子從 0 遞增至 1,RGB 通道漸顯,Alpha 通道同步漸顯。

完整程式碼


private void btnFadeIn_Click(object sender, EventArgs e)
{
    Graphics g = panel1.CreateGraphics();
    g.Clear(Color.Gray);
    int w = MyBitmap.Width, h = MyBitmap.Height;

    var attrs = new ImageAttributes();
    var matrix = new ColorMatrix
    {
        Matrix33 = 1,
        Matrix44 = 1
    };

    float factor = 0.0f;
    while (factor < 1.0f)
    {
        matrix.Matrix00 = matrix.Matrix11 = matrix.Matrix22 = matrix.Matrix33 = factor;
        attrs.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

        g.DrawImage(MyBitmap, new Rectangle(0, 0, w, h), 0, 0, w, h, GraphicsUnit.Pixel, attrs);
        Thread.Sleep(200);
        factor += 0.02f;
    }
}

關鍵參數

  • Thread.Sleep(200):淡入速度較慢(200ms/步),淡出較快(20ms/步),視覺上更自然
  • factor += 0.02f:每步增量,共 50 步完成
  • ColorAdjustType.Bitmap:套用於位圖繪製

ImageAttributes 進階用途

  • 色彩校正(亮度、對比、伽瑪)
  • 灰階轉換
  • 色調旋轉
  • 透明色鍵控
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved