參考 PowerPoint 簡報動畫效果,使用 GDI+ 實作 10 種圖片動畫顯示方式。所有動畫邏輯封裝在 PictureBox 控制項上,共用基礎架構程式碼。
public partial class MainForm : Form
{
private Bitmap _sourceBitmap; // 原始尺寸
private Bitmap _displayBitmap; // 縮放至 PictureBox 尺寸
private void btnLoadImage_Click(object sender, EventArgs e)
{
using (var dlg = new OpenFileDialog())
{
dlg.Filter = "圖片檔案|*.jpg;*.jpeg;*.gif;*.bmp;*.tif;*.tiff;*.png|所有檔案|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
_sourceBitmap = new Bitmap(dlg.FileName);
_displayBitmap = new Bitmap(_sourceBitmap, pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = _displayBitmap;
}
}
}
}
每個動畫對應一個按鈕 Click 事件,內部使用 Graphics 直接繪製於 Panel 或 PictureBox,配合 Thread.Sleep 控制幀率。教學用途,生產環境建議使用 Timer 或 async/await Task.Delay 避免阻塞 UI 執行緒。
| 類別 | 命名空間 | 主要功能 |
|---|---|---|
| Graphics | System.Drawing | 繪圖介面,DrawImage、FillRectangle、RotateTransform 等 |
| Bitmap | System.Drawing | 像素資料,Clone、RotateFlip、GetPixel、SetPixel |
| TextureBrush | System.Drawing | 圖片紋理筆刷,支援幾何變換 |
| ColorMatrix | System.Drawing.Imaging | 5x5 色彩變換矩陣 |
| ImageAttributes | System.Drawing.Imaging | 繪圖屬性,SetColorMatrix、SetColorKey 等 |