private void btnMeetMiddle_Click(object sender, EventArgs e)
{
int w = MyBitmap.Width, h = MyBitmap.Height;
Graphics g = panel1.CreateGraphics();
g.Clear(Color.Gray);
var canvas = new Bitmap(w, h);
for (int x = 0; x <= w / 2; x++)
{
for (int y = 0; y < h; y++)
{
canvas.SetPixel(x, y, MyBitmap.GetPixel(x, y));
canvas.SetPixel(w - x - 1, y, MyBitmap.GetPixel(w - x - 1, y));
}
panel1.Refresh();
g.DrawImage(canvas, 0, 0);
Thread.Sleep(10);
}
}
從左右兩邊同時向中間繪製像素,產生對接效果。注意:SetPixel/GetPixel 極慢,僅供示範。
private void btnFlipHorizontal_Click(object sender, EventArgs e)
{
int w = MyBitmap.Width, h = MyBitmap.Height;
Graphics g = panel1.CreateGraphics();
g.Clear(Color.Gray);
for (int j = -h/2; j <= h/2; j++)
{
int i = (int)(j * (float)w / h);
var dest = new Rectangle(w/2 - i, 0, 2*i, h);
var src = new Rectangle(0, 0, w, h);
g.DrawImage(MyBitmap, dest, src, GraphicsUnit.Pixel);
Thread.Sleep(10);
}
}
以高度中心為軸,水平鏡像擴展繪製。
// 左右對接:使用 DrawImage 繪製兩半
g.DrawImage(MyBitmap, new Rectangle(0, 0, x, h), 0, 0, x, h, GraphicsUnit.Pixel); // 左半
g.DrawImage(MyBitmap, new Rectangle(w-x, 0, x, h), w-x, 0, x, h, GraphicsUnit.Pixel); // 右半
// 左右反轉:直接用 RotateFlip
MyBitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);
g.DrawImage(MyBitmap, 0, 0);
生產環境請使用 DrawImage 區域繪製或 RotateFlip,避免逐像素操作。