private void btnExpand_Click(object sender, EventArgs e)
{
int w = MyBitmap.Width, h = MyBitmap.Height;
Graphics g = panel1.CreateGraphics();
g.Clear(Color.Gray);
for (int i = 0; i <= w / 2; i++)
{
int j = (int)(i * (float)h / w); // 保持長寬比
var dest = new Rectangle(w/2 - i, h/2 - j, 2*i, 2*j);
var src = new Rectangle(0, 0, w, h);
g.DrawImage(MyBitmap, dest, src, GraphicsUnit.Pixel);
Thread.Sleep(10);
}
}
從中心點向四周按比例擴大繪製矩形,產生放大動畫。
private void btnBlock_Click(object sender, EventArgs e)
{
Graphics g = panel1.CreateGraphics();
g.Clear(Color.White);
int w = MyBitmap.Width, h = MyBitmap.Height;
// 切割 4 區塊
var blocks = new RectangleF[]
{
new RectangleF(0, 0, w/2, h/2),
new RectangleF(w/2, 0, w/2, h/2),
new RectangleF(0, h/2, w/2, h/2),
new RectangleF(w/2, h/2, w/2, h/2)
};
var pieces = blocks.Select(b => MyBitmap.Clone(b, PixelFormat.DontCare)).ToArray();
g.DrawImage(pieces[0], 0, 0); Thread.Sleep(1000);
g.DrawImage(pieces[1], w/2, 0); Thread.Sleep(1000);
g.DrawImage(pieces[3], w/2, h/2); Thread.Sleep(1000);
g.DrawImage(pieces[2], 0, h/2);
}
將圖片四等分,依序繪製每塊,間隔 1 秒,產生拼圖組裝效果。