首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 圖片動畫顯示技巧(六):左右對接與左右反轉

     編輯:關於C語言

左右對接效果


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,避免逐像素操作。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved