private void btnStretchTopDown_Click(object sender, EventArgs e)
{
int w = MyBitmap.Width, h = MyBitmap.Height;
Graphics g = panel1.CreateGraphics();
g.Clear(Color.Gray);
for (int y = 1; y <= h; y++)
{
using (var part = MyBitmap.Clone(new Rectangle(0, 0, w, y), PixelFormat.Format24bppRgb))
{
g.DrawImage(part, 0, 0);
}
Thread.Sleep(10);
}
}
每次增加一行高度,Bitmap.Clone 裁切指定矩形區域,產生逐行展開效果。
private void btnStretchLeftRight_Click(object sender, EventArgs e)
{
int w = MyBitmap.Width, h = MyBitmap.Height;
Graphics g = panel1.CreateGraphics();
g.Clear(Color.Gray);
for (int x = 1; x <= w; x++)
{
using (var part = MyBitmap.Clone(new Rectangle(0, 0, x, h), PixelFormat.Format24bppRgb))
{
g.DrawImage(part, 0, 0);
}
Thread.Sleep(10);
}
}
同理逐欄增加寬度。
Bitmap.Clone 建立新物件,頻繁呼叫會產生大量暫時物件Bitmap,用 Graphics.DrawImage 繪製部分區域,避免重複配置TextureBrush + FillRectangle 配合裁切矩形