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

C# 圖片動畫顯示技巧(七):上下拉伸與左右拉伸

     編輯:關於C語言

從上向下拉伸展開


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 配合裁切矩形
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved