這是一個比較複雜的程序,包含了30種圖像動畫特效演示,使用C#編寫,源程序大概2000多行。
這個軟件實際上主要是四個方面的內容:
一、30種動畫特效算法,包含諸如隨機拉絲、交替分塊、多經掃描等等。這些算法設計的比較巧妙,也就是說大量的使用了圖像處理的一些技巧。
二、.NET的GDI+技術功能很是強大,本軟件中幾乎涉及了GDI+中的各個方面,例如仿射變換矩陣、顏色變換矩陣、塊處理等等。
三、採用多線程技術,使得軟件看起來比較有序,同時採用信號量來實現暫停、繼續、取消等功能。
四、採用比較嚴謹的面向對象的程序設計技術,從類的定義到方法的、事件的定義都嚴格按照OOP理論完成,能夠說比較完整、精確的體現了OOP精髓。
這是截屏動畫效果:
- using System;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Imaging;
- using System.Threading;
- using System.Windows.Forms;
- namespace Mengliao.CSharp.A14
- {
- #region 動畫類型枚舉
- // 本例爲了簡單起見沒有使用見名知意的名稱,如用於實際可將這些枚舉成員改名爲準確的英文名稱
- enum AnimateType
- {
- Animator01, Animator02, Animator03, Animator04, Animator05,
- Animator06, Animator07, Animator08, Animator09, Animator10,
- Animator11, Animator12, Animator13, Animator14, Animator15,
- Animator16, Animator17, Animator18, Animator19, Animator20,
- Animator21, Animator22, Animator23, Animator24, Animator25,
- Animator26, Animator27, Animator28, Animator29, Animator30
- }
- #endregion
- class AnimatorImage
- {
- #region 私有字段
- // 輸入位圖
- private Bitmap bmp;
- // 是否已經開始繪製
- private bool drawStarted = false;
- // 在繪製過程當中是否終止的自動復位信號量,有信號則終止
- private AutoResetEvent cancelEvent = new AutoResetEvent(false);
- // 在繪製過程當中是否暫停的手動復位信號量,有信號則暫停
- private ManualResetEvent pauseEvent = new ManualResetEvent(false);
- // 輸出位圖的DC
- private Graphics dc;
- #endregion
- #region 屬性和事件
- private Bitmap outBmp;
- /// <summary>
- /// 輸出位圖。
- /// </summary>
- public Bitmap OutBmp
- {
- get { return outBmp; }
- }
- private int delay;
- /// <summary>
- /// 延時係數。
- /// </summary>
- public int Delay
- {
- get { return delay; }
- set { delay = Math.Min(Math.Max(1, value), 100); } // 使其介於1到100之間
- }
- /// <summary>
- /// 重繪事件。
- /// </summary>
- public event PaintEventHandler Redraw;
- protected void OnRedraw(Rectangle clipRectangle)
- {
- if (Redraw != null)
- {
- Redraw.Invoke(this, new PaintEventArgs(dc, clipRectangle));
- }
- }
- /// <summary>
- /// 繪製開始事件。
- /// </summary>
- public event EventHandler DrawStarted;
- protected void OnDrawStarted(object sender, EventArgs e)
- {
- drawStarted = true;
- if (DrawStarted != null)
- {
- DrawStarted.Invoke(sender, e);
- }
- }
- /// <summary>
- /// 繪製完成事件。
- /// </summary>
- public event EventHandler DrawCompleted;
- protected void OnDrawCompleted(object sender, EventArgs e)
- {
- drawStarted = false;
- cancelEvent.Reset();
- if (DrawCompleted != null)
- {
- DrawCompleted.Invoke(sender, e);
- }
- }
- #endregion
- #region 私有方法
- // 在輸出位圖上顯示繪製過程當中的錯誤信息
- private void ShowError(string errMsg)
- {
- Font font = new Font("宋體", 9);
- SizeF size = dc.MeasureString(errMsg, font);
- PointF point = new PointF((outBmp.Width - size.Width) / 2f, (outBmp.Height - size.Height) / 2f);
- // 在文字的四個方向各一個像素處繪製其它顏色的文字,以造成邊框,不然可能看不清除文字
- dc.DrawString(errMsg, font, Brushes.Red, point.X - 1f, point.Y);
- dc.DrawString(errMsg, font, Brushes.Red, point.X + 1f, point.Y);
- dc.DrawString(errMsg, font, Brushes.Red, point.X, point.Y - 1f);
- dc.DrawString(errMsg, font, Brushes.Red, point.X, point.Y + 1f);
- // 繪製文字
- dc.DrawString(errMsg, font, Brushes.White, point);
- ShowBmp(new Rectangle(Point.Round(point), Size.Round(size)));
- }
- // 供繪製動畫方法內部調用,三個重載版本
- private void ShowBmp(Rectangle clipRectangle)
- {
- string cancelMsg = "繪圖操做已被用戶取消!";
- OnRedraw(clipRectangle);
- if (cancelEvent.WaitOne(0)) // 取消
- {
- // 該異常將被外部方法捕獲,即各個繪製方法
- throw new ApplicationException(cancelMsg);
- }
- while (pauseEvent.WaitOne(0)) // 暫停
- {
- if (cancelEvent.WaitOne(10)) // 在暫停期間取消
- {
- pauseEvent.Reset();
- throw new ApplicationException(cancelMsg);
- }
- }
- }
- private void ShowBmp(RectangleF clipRectangle) // 接收浮點參數
- {
- ShowBmp(Rectangle.Round(clipRectangle));
- }
- private void ShowBmp() // 重繪所有區域
- {
- ShowBmp(new Rectangle(0, 0, bmp.Width, bmp.Height));
- }
- // 清空背景
- private void ClearBackground()
- {
- dc.Clear(Color.FromKnownColor(KnownColor.ButtonFace)); // 置背景色
- ShowBmp(); // 重繪全部區域
- }
- #endregion
- #region 動畫控制
- /// <summary>
- /// 取消繪製。
- /// </summary>
- public void CancelDraw()
- {
- if (drawStarted)
- {
- cancelEvent.Set();
- }
- }
- /// <summary>
- /// 暫停繪製。
- /// </summary>
- public void PauseDraw()
- {
- if (drawStarted)
- {
- pauseEvent.Set();
- }
- }
- /// <summary>
- /// 繼續繪製。
- /// </summary>
- public void ResumeDraw()
- {
- if (drawStarted)
- {
- pauseEvent.Reset();
- }
- }
- #endregion
- #region 構造函數
- /// <summary>
- /// 實例化後,需分配事件處理方法,全部事件均在獨立線程中觸發;默認延時係數爲1。
- /// </summary>
- /// <param name="inBmp">輸入位圖</param>
- public AnimatorImage(Bitmap inBmp)
- {
- delay = 1;
- this.bmp = (Bitmap)inBmp.Clone();
- outBmp = new Bitmap(this.bmp.Width, this.bmp.Height);
- dc = Graphics.FromImage(outBmp);
- }
- #endregion
- #region 繪製動畫
- /// <summary>
- /// 以獨立線程的方式開始顯示動畫。
- /// </summary>
- /// <param name="animateType">動畫類型枚舉</param>
- public void DrawAnimator(AnimateType animateType)
- {
- if (drawStarted) // 判斷動畫是否已經開始繪製
- {
- if (pauseEvent.WaitOne(0)) // 動畫已開始,但被暫停了,繼續
- pauseEvent.Reset();
- else
- pauseEvent.Set();
- return;
- }
- ThreadStart threadMethod;
- switch (animateType)
- {
- case AnimateType.Animator01:
- threadMethod = Animator01;
- break;
- case AnimateType.Animator02:
- threadMethod = Animator02;
- break;
- case AnimateType.Animator03:
- threadMethod = Animator03;
- break;
- case AnimateType.Animator04:
- threadMethod = Animator04;
- break;
- case AnimateType.Animator05:
- threadMethod = Animator05;
- break;
- case AnimateType.Animator06:
- threadMethod = Animator06;
- break;
- case AnimateType.Animator07:
- threadMethod = Animator07;
- break;
- case AnimateType.Animator08:
- threadMethod = Animator08;
- break;
- case AnimateType.Animator09:
- threadMethod = Animator09;
- break;
- case AnimateType.Animator10:
- threadMethod = Animator10;
- break;
- case AnimateType.Animator11:
- threadMethod = Animator11;
- break;
- case AnimateType.Animator12:
- threadMethod = Animator12;
- break;
- case AnimateType.Animator13:
- threadMethod = Animator13;
- break;
- case AnimateType.Animator14:
- threadMethod = Animator14;
- break;
- case AnimateType.Animator15:
- threadMethod = Animator15;
- break;
- case AnimateType.Animator16:
- threadMethod = Animator16;
- break;
- case AnimateType.Animator17:
- threadMethod = Animator17;
- break;
- case AnimateType.Animator18:
- threadMethod = Animator18;
- break;
- case AnimateType.Animator19:
- threadMethod = Animator19;
- break;
- case AnimateType.Animator20:
- threadMethod = Animator20;
- break;
- case AnimateType.Animator21:
- threadMethod = Animator21;
- break;
- case AnimateType.Animator22:
- threadMethod = Animator22;
- break;
- case AnimateType.Animator23:
- threadMethod = Animator23;
- break;
- case AnimateType.Animator24:
- threadMethod = Animator24;
- break;
- case AnimateType.Animator25:
- threadMethod = Animator25;
- break;
- case AnimateType.Animator26:
- threadMethod = Animator26;
- break;
- case AnimateType.Animator27:
- threadMethod = Animator27;
- break;
- case AnimateType.Animator28:
- threadMethod = Animator28;
- break;
- case AnimateType.Animator29:
- threadMethod = Animator29;
- break;
- default:
- threadMethod = Animator30;
- break;
- }
- Thread drawThread = new Thread(threadMethod);
- drawThread.IsBackground = true; // 設爲後臺線程,避免該線程未結束時退出主線程而引起異常
- drawThread.Start();
- }
- #endregion
- // ==========
- // 動畫算法
- // ==========
- #region 壓縮反轉(改進版)
- // 原理:計算圖像位置和高度,以高度的一半爲軸進行對換上下半邊的圖像
- private void Animator01()
- {
- const float blockSize = 8; // 每次顯示的高度增量,應能被高度整除
- try
- {
- OnDrawStarted(this, EventArgs.Empty); // 觸發開始繪製事件
- //ClearBackground();
- Color bgColor = Color.FromKnownColor(KnownColor.ButtonFace);
- RectangleF srcRect = new RectangleF(0, 0, bmp.Width, bmp.Height);
- for (float i = (float)Math.Floor(-bmp.Height / blockSize); i <= Math.Ceiling(bmp.Height / blockSize); i++)
- {
- dc.Clear(bgColor); // 清空DC
- float j = i * blockSize / 2;
- float destTop = bmp.Height / 2 - j; // 目標矩形的頂位置
- // 目標矩形區域在循環的前半段爲垂直反向
- RectangleF destRect = new RectangleF(0, destTop, bmp.Width, 2 * j);
- // 在指定區域繪製圖像,該圖像被拉伸
- dc.DrawImage(bmp, destRect, srcRect, GraphicsUnit.Pixel);
- ShowBmp();
- Thread.Sleep(10 * delay); // 休眠
- }
- }
- catch (Exception ex)
- {
- ShowError(ex.Message);
- }
- finally
- {
- OnDrawCompleted(this, EventArgs.Empty); // 觸發完成繪製事件
- }
- }
- #endregion
- #region 垂直對接(改進版)
- // 原理:將圖像分爲上下部分,而後同時向中心移動
- private void Animator02()
- {
- const int stepCount = 4; // 每次上下移動的步長像素,應能被高度整除
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
- Rectangle sourTopRect = new Rectangle(0, 0, bmp.Width, bmp.Height / 2); // 上半部分源區域
- Rectangle sourBottRect = new Rectangle(0, bmp.Height / 2, bmp.Width, bmp.Height / 2); // 下半部分源區域
- for (int i = 0; i <= bmp.Height / 2; i += stepCount)
- {
- Rectangle destTopRect = new Rectangle(0, i - bmp.Height / 2 + 1, bmp.Width, bmp.Height / 2); // 上半部分目標區域
- Rectangle destBottRect = new Rectangle(0, bmp.Height - i - 1, bmp.Width, bmp.Height / 2); // 下半部分目標區域
- dc.DrawImage(bmp, destTopRect, sourTopRect, GraphicsUnit.Pixel);
- dc.DrawImage(bmp, destBottRect, sourBottRect, GraphicsUnit.Pixel);
- ShowBmp(Rectangle.Union(destTopRect, destBottRect));
- Thread.Sleep(10 * delay);
- }
- }
- catch (Exception ex)
- {
- ShowError(ex.Message);
- }
- finally
- {
- OnDrawCompleted(this, EventArgs.Empty);
- }
- }
- #endregion
- #region 中心閉幕(改進版)
- // 原理:由大到小生成圖像中心區域,而後用總區域減去該中心區域,並用材質畫刷填充
- private void Animator03()
- {
- const float stepCount = 4; // 每次收縮的步長像素
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
- // 創建空區域,如使用Region的無參構造函數則創建一個無限大小的區域,而非空區域
- Region region = new Region(new GraphicsPath());
- // 創建位圖材質畫刷
- TextureBrush textureBrush = new TextureBrush(bmp);
- for (float x = 0; x <= bmp.Width / 2f; x += stepCount)
- {
- // 添加整個位圖區域
- region.Union(new Rectangle(0, 0, bmp.Width, bmp.Height));
- // 從中心開始,由大到小填充背景色或填充縮小尺寸的原圖
- // 計算高度變化量,若是寬度大,則高度變化量小於寬度,不然大於寬度
- float y = x * bmp.Height / bmp.Width;
- RectangleF rect = new RectangleF(x, y, bmp.Width - 2f * x, bmp.Height - 2f * y);
- // 計算整個位圖區域與背景色區域的差集
- region.Exclude(rect);
- dc.FillRegion(textureBrush, region); // 使用材質畫刷填充區域
- ShowBmp(region.GetBounds(dc));
- Thread.Sleep(10 * delay);
- }
- // 因爲stepCount可能沒法被寬度整除,則最終生成的背景色區域並不爲空,故在循環結束後繪製整個位圖
- dc.DrawImage(bmp, 0, 0);
- ShowBmp();
- }
- catch (Exception ex)
- {
- ShowError(ex.Message);
- }
- finally
- {
- OnDrawCompleted(this, EventArgs.Empty);
- }
- }
- #endregion
- #region 中心放大(改進版)
- // 原理:由中心向邊緣按高度和寬度的比例循環輸出全部像素,直到高度和寬度爲原始大小
- private void Animator04()
- {
- const int stepCount = 4; // 每次增長的像素量,應能被寬度整除
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
- Rectangle sourRect = new Rectangle(0, 0, bmp.Width, bmp.Height); // 源區域爲整個位圖
- for (int i = 0; i <= bmp.Width / 2; i += stepCount)
- {
- int j = i * bmp.Height / bmp.Width; // 計算高度變化量,若是寬度大,則高度變化量小於寬度,不然大於寬度
- Rectangle destRect = new Rectangle(bmp.Width / 2 - i, bmp.Height / 2 - j, 2 * i, 2 * j);
- dc.DrawImage(bmp, destRect, sourRect, GraphicsUnit.Pixel);
- ShowBmp(destRect);
- Thread.Sleep(10 * delay);
- }
- }
- catch (Exception ex)
- {
- ShowError(ex.Message);
- }
- finally
- {
- OnDrawCompleted(this, EventArgs.Empty);
- }
- }
- #endregion
- #region 逐行分塊
- // 原理:將圖像分爲正方形塊,而後從左到右,從上到下順序顯示
- private void Animator05()
- {
- const float blockSize = 50; // 正方塊的邊長
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
- // 防止最後一列、最後一行不足一塊的尺寸而不顯示,故採用上取整
- for (int y = 0; y < Math.Ceiling(bmp.Height / blockSize); y++)
- {
- for (int x = 0; x < Math.Ceiling(bmp.Width / blockSize); x++)
- {
- RectangleF rect;
- if (y % 2 == 0) // 從左到右
- {
- rect = new RectangleF(x * blockSize, y * blockSize, blockSize, blockSize);
- }
- else // 從右到左
- {
- rect = new RectangleF((bmp.Width / blockSize - x - 1) * blockSize, y * blockSize, blockSize, blockSize);
- }
- dc.DrawImage(bmp, rect, rect, GraphicsUnit.Pixel);
- ShowBmp(rect);
- Thread.Sleep(10 * delay);
- }
- }
- }
- catch (Exception ex)
- {
- ShowError(ex.Message);
- }
- finally
- {
- OnDrawCompleted(this, EventArgs.Empty);
- }
- }
- #endregion
- #region 交替分塊(改進版)
- // 原理:將圖像分爲正方形塊,而後計算全部分塊按照奇偶從左到右顯示或從右到左顯示所需的區域,並用材質畫刷填充
- private void Animator06()
- {
- const float blockSize = 70; // 正方塊的邊長
- const int showWidth = 1; // 每次顯示的像素列數
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
- // 創建空區域,如使用Region的無參構造函數則創建一個無限大小的區域,而非空區域
- Region region = new Region(new GraphicsPath());
- // 創建位圖材質畫刷
- TextureBrush textureBrush = new TextureBrush(bmp);
- // 分塊的行座標+列座標爲偶數則從左到右逐列顯示本塊,不然從右到左逐列顯示本塊
- for (int i = 0; i <= Math.Ceiling(blockSize / showWidth); i++)
- {
- for (int x = 0; x < Math.Ceiling(bmp.Width / blockSize); x++)
- {
- for (int y = 0; y < Math.Ceiling(bmp.Height / blockSize); y++)
- {
- RectangleF rect;
- // 判斷塊的行列座標和爲奇數或偶數
- if ((x + y) % 2 == 0)
- {
- rect = new RectangleF(x * blockSize + i * showWidth, y * blockSize, showWidth, blockSize);
- }
- else
- {
- rect = new RectangleF((x + 1) * blockSize - i * showWidth, y * blockSize, showWidth, blockSize);
- }
- region.Union(rect); // 將要顯示的區域合併到region中
- }
- }
- dc.FillRegion(textureBrush, region); // 使用材質畫刷填充區域
- ShowBmp(region.GetBounds(dc));
- Thread.Sleep(10 * delay);
- }
- }
- catch (Exception ex)
- {
- ShowError(ex.Message);
- }
- finally
- {
- OnDrawCompleted(this, EventArgs.Empty);
- }
- }
- #endregion
- #region 交叉豎條(改進版)
- // 原理:將圖像分紅寬度相等的列,而後計算從上下兩個方向交叉前進的區域,並使用材質畫刷填充
- private void Animator07()
- {
- const float lineWidth = 4; // 豎條寬度
- const float lineStep = 6; // 豎條每次前進的步長
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
- GraphicsPath path = new GraphicsPath(); // 創建路徑,路徑處理速度要明顯快於Region,但不支持集合運算
- TextureBrush textureBrush = new TextureBrush(bmp);
- // 從上到下和從下到上以步長爲單位顯示
- for (int y = 0; y < Math.Ceiling(bmp.Height / lineStep); y++)
- {
- // 顯示兩個方向的每一個垂直豎條
- for (int x = 0; x < Math.Ceiling(bmp.Width / lineWidth); x++)
- {
- RectangleF rect;
- if (x % 2 == 0) // 從上到下
- {
- rect = new RectangleF(x * lineWidth, y * lineStep, lineWidth, lineStep);
- }
- else // 從下到上
- {
- rect = new RectangleF(x * lineWidth, bmp.Height - y * lineStep - lineStep, lineWidth, lineStep);
- }
- path.AddRectangle(rect);
- }
- dc.FillPath(textureBrush, path);
- ShowBmp(path.GetBounds());
- Thread.Sleep(10 * delay);
- }
- }
- catch (Exception ex)
- {
- ShowError(ex.Message);
- }
- finally
- {
- OnDrawCompleted(this, EventArgs.Empty);
- }
- }
- #endregion
- #region 透明淡入(改進版)
- // 原理:使用ImageAttributes類和顏色轉換矩陣處理圖像,使每一個像素的顏色份量同步增長
- private void Animator08()
- {
- const float stepCount = 0.02f; // 顏色轉換矩陣增量,該值除1應等於整數
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
- // ImageAttributes類的實例用於調整顏色,由DrawImage()方法調用
- ImageAttributes attributes = new ImageAttributes();
- // 創建5*5階RGBA顏色矩陣
- ColorMatrix matrix = new ColorMatrix();
- float value = 0;
- while (value < 1f)
- {
- matrix.Matrix33 = value;
- // 爲ImageAttributes對象指定顏色調整矩陣
- // ColorMatrixFlag.Default表示使用矩陣調整全部顏色;ColorAdjustType.Bitmap表示調整位圖的顏色
- attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
- dc.Clear(Color.FromKnownColor(KnownColor.ButtonFace)); // 清空DC,不然每次會將不一樣的透明度圖像疊加顯示
- dc.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
- value += stepCount;
- ShowBmp();
- Thread.Sleep(10 * delay);
- }
- }
- catch (Exception ex)
- {
- ShowError(ex.Message);
- }
- finally
- {
- OnDrawCompleted(this, EventArgs.Empty);
- }
- }
- #endregion
- #region 三色淡入
- // 原理:使用ImageAttributes類和顏色轉換矩陣處理圖像,分三次增長每一個像素的單個顏色份量
- private void Animator09()
- {
- const float stepCount = 0.025f; // 顏色轉換矩陣增量,該值除1應等於整數
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
- // ImageAttributes類的實例用於調整顏色,由DrawImage()方法調用
- ImageAttributes attributes = new ImageAttributes();
- // 創建5*5階RGBA顏色矩陣
- ColorMatrix matrix = new ColorMatrix();
- matrix.Matrix00 = 0f; // R爲0
- matrix.Matrix11 = 0f; // G爲0
- matrix.Matrix22 = 0f; // B爲0
- // 如下三個循環依次處理B、R、G,符合亮度方程的原理
- // 人眼對B最不敏感,對G最敏感,或者說B傳達的亮度信息最少,G傳達的亮度信息最多
- // 所以先處理亮度信息少的,最後處理亮度信息多的,若是反過來處理,則變化不明顯
- float value = 0f;
- while (value < 1f)
- {
- matrix.Matrix22 = value; // 顏色R的轉換矩陣份量值
- // 爲ImageAttributes對象指定顏色調整矩陣
- // ColorMatrixFlag.Default表示使用矩陣調整全部顏色;ColorAdjustType.Bitmap表示調整位圖的顏色
- attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
- dc.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
- value += stepCount;
- ShowBmp();
- Thread.Sleep(10 * delay);
- }
- value = stepCount;
- while (value < 1f)
- {
- matrix.Matrix00 = value; // 顏色G的轉換矩陣份量值
- attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
- dc.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
- value += stepCount;
- ShowBmp();
- Thread.Sleep(10 * delay);
- }
- value = stepCount;
- while (value < 1f)
- {
- matrix.Matrix11 = value; // 顏色B的轉換矩陣份量值
- attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
- dc.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
- value += stepCount;
- ShowBmp();
- Thread.Sleep(10 * delay);
- }
- }
- catch (Exception ex)
- {
- ShowError(ex.Message);
- }
- finally
- {
- OnDrawCompleted(this, EventArgs.Empty);
- }
- }
- #endregion
- #region 水平拉幕
- // 原理:由中心向開始逐漸輸出中心兩側的像素,直到寬度爲原始大小
- private void Animator10()
- {
- const int stepCount = 4; // 每次增長的步長像素,該值應能被寬度整除
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
- for (int i = 0; i <= Math.Ceiling(bmp.Width / 2f); i += stepCount)
- {
- Rectangle rect = new Rectangle(bmp.Width / 2 - i, 0, 2 * i, bmp.Height);
- dc.DrawImage(bmp, rect, rect, GraphicsUnit.Pixel);
- ShowBmp(rect);
- Thread.Sleep(10 * delay);
- }
- }
- catch (Exception ex)
- {
- ShowError(ex.Message);
- }
- finally
- {
- OnDrawCompleted(this, EventArgs.Empty);
- }
- }
- #endregion
本文的最後一部分(下),會附上完整的項目文件組,包含全部資源及可執行文件。
這裏是本文的第二部分:http://mengliao.blog.51cto.com/876134/473193算法