對角線縮放因子從 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:套用於位圖繪製