淡出動畫邏輯與淡入相反:主對角線值從 1 遞減至 0,RGB 飽和度逐漸歸零。
private void btnFadeOut_Click(object sender, EventArgs e) {
Graphics g = panel1.CreateGraphics();
g.Clear(Color.Gray);
int w = MyBitmap.Width, h = MyBitmap.Height;
ImageAttributes attr = new ImageAttributes();
ColorMatrix cm = new ColorMatrix();
cm.Matrix33 = cm.Matrix44 = 1f;
for (float c = 1f; c > 0f; c -= 0.01f) {
cm.Matrix00 = cm.Matrix11 = cm.Matrix22 = cm.Matrix33 = c;
attr.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(MyBitmap, new Rectangle(0, 0, w, h), 0, 0, w, h, GraphicsUnit.Pixel, attr);
Thread.Sleep(20);
}
}
| 特性 | 淡入 | 淡出 |
|---|---|---|
| 主對角線初值 | 0 | 1 |
| 變化方向 | 遞增 | 遞減 |
| 視覺效果 | 透明→不透明 | 不透明→透明 |