利用 `ImageAttributes.SetColorMatrix` 動態調整顏色矩陣主對角線值(0→1),實現平滑淡入動畫。
private void btnFadeIn_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();
// 初始化為零矩陣,保留 Alpha 通道
cm.Matrix33 = cm.Matrix44 = 1f;
for (float c = 0f; c < 1f; c += 0.02f) {
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); // 控制幀率
}
}