這是截屏動畫效果:
- #region 隨機豎條
- // 原理:將圖像分紅寬度相等的列,而後隨機選擇每一列並從上到下顯示
- private void Animator11()
- {
- const float lineWidth = 40; // 豎條寬度
- const int stepCount = 12; // 豎條每次前進量
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
- Random rnd = new Random(); // 隨機數類
- // 生成每一個列隨機顯示的次序
- int[] colIndex = new int[(int)Math.Ceiling(bmp.Width / lineWidth)];
- int index = 1; // 數組索引
- // 數組被自動初始化爲0,所以能夠經過判斷其中的元素是否爲0而得知該位置是否產生了隨機數
- // 爲了區別自動初始化的元素值,index從1開始
- do
- {
- int s = rnd.Next(colIndex.Length);
- if (colIndex[s] == 0)
- {
- colIndex[s] = index++;
- }
- } while (index <= colIndex.Length);
- // 按照上面隨機生成的次序逐一顯示每一個豎條
- for (int i = 0; i < colIndex.Length; i++)
- {
- for (int y = 0; y < bmp.Height; y += stepCount)
- {
- RectangleF rect = new RectangleF((colIndex[i] - 1) * lineWidth, y, lineWidth, stepCount);
- dc.DrawImage(bmp, rect, rect, GraphicsUnit.Pixel);
- Thread.Sleep(1 * delay);
- ShowBmp(rect);
- }
- Thread.Sleep(10 * delay);
- }
- }
- catch (Exception ex)
- {
- ShowError(ex.Message);
- }
- finally
- {
- OnDrawCompleted(this, EventArgs.Empty);
- }
- }
- #endregion
- #region 隨機拉絲
- // 原理:每次隨機顯示圖像的一個像素行
- private void Animator12()
- {
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
- Random rnd = new Random(); // 隨機數類
- // 生成每一個像素行的顯示次序
- int[] rowIndex = new int[bmp.Height];
- int index = 1; // 數組索引
- do
- {
- int s = rnd.Next(rowIndex.Length);
- if (rowIndex[s] == 0)
- {
- rowIndex[s] = index++;
- }
- } while (index <= rowIndex.Length);
- // 按照上面隨機生成的次序逐一顯示每一個像素行
- for (int i = 0; i < rowIndex.Length; i++)
- {
- RectangleF rect = new RectangleF(0, (rowIndex[i] - 1), bmp.Width, 1);
- dc.DrawImage(bmp, rect, rect, GraphicsUnit.Pixel);
- ShowBmp(rect);
- Thread.Sleep(1 * delay);
- }
- }
- catch (Exception ex)
- {
- ShowError(ex.Message);
- }
- finally
- {
- OnDrawCompleted(this, EventArgs.Empty);
- }
- }
- #endregion
- #region 垂直對切
- // 原理:由圖像中心向分左右兩半分別向上下兩個方向顯示,到達邊緣後按一樣方向補齊另外的部分
- private void Animator13()
- {
- const int stepCount = 4; // 上下前進的增量像素,應能被高度整除
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
- // 第一次循環,左半部分從垂直中心向上顯示,右半部分從垂直中心向下顯示
- for (int y = 0; y <= bmp.Height / 2; y += stepCount)
- {
- // 左半部分
- Rectangle rectLeft = new Rectangle(0, bmp.Height / 2 - y - stepCount, bmp.Width / 2, stepCount);
- dc.DrawImage(bmp, rectLeft, rectLeft, GraphicsUnit.Pixel);
- // 右半部分
- Rectangle rectRight = new Rectangle(bmp.Width / 2, bmp.Height / 2 + y, bmp.Width / 2, stepCount);
- dc.DrawImage(bmp, rectRight, rectRight, GraphicsUnit.Pixel);
- ShowBmp(Rectangle.Union(rectLeft, rectRight));
- Thread.Sleep(10 * delay);
- }
- // 第二次循環,左半部分從底邊向上顯示,右半部分從頂邊向下顯示
- for (int y = 0; y <= bmp.Height / 2; y += stepCount)
- {
- // 左半部分
- Rectangle rectLeft = new Rectangle(0, bmp.Height - y - stepCount, bmp.Width / 2, stepCount);
- dc.DrawImage(bmp, rectLeft, rectLeft, GraphicsUnit.Pixel);
- // 右半部分
- Rectangle rectRight = new Rectangle(bmp.Width / 2, y, bmp.Width / 2, stepCount);
- dc.DrawImage(bmp, rectRight, rectRight, GraphicsUnit.Pixel);
- ShowBmp(Rectangle.Union(rectLeft, rectRight));
- Thread.Sleep(10 * delay);
- }
- }
- catch (Exception ex)
- {
- ShowError(ex.Message);
- }
- finally
- {
- OnDrawCompleted(this, EventArgs.Empty);
- }
- }
- #endregion
- #region 隨機分塊
- // 原理:框像分割爲相等的正方塊,而後逐個隨機顯示其中之一,直到所有顯示完成
- private void Animator14()
- {
- const float blockSize = 50; // 分塊尺寸,若是該尺寸爲4到6時,顯示效果爲隨機圖點
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
- Random rnd = new Random(); // 隨機數類
- // 定義二維數組,對應每一個分塊,其中保存該塊的位置索引(基於總塊數)
- int[,] blockIndex = new int[(int)Math.Ceiling(bmp.Width / blockSize), (int)Math.Ceiling(bmp.Height / blockSize)];
- // 生成隨機快座標,並填充順序號
- int s = 1; // 分塊次序(從左到右,從上到下)
- do
- {
- int x = rnd.Next(blockIndex.GetLength(0));
- int y = rnd.Next(blockIndex.GetLength(1));
- if (blockIndex[x, y] == 0)
- {
- blockIndex[x, y] = s++;
- }
- } while (s <= blockIndex.GetLength(0) * blockIndex.GetLength(1));
- // 按照上面隨機生成的次序逐一顯示全部分塊
- for (int x = 0; x < blockIndex.GetLength(0); x++)
- {
- for (int y = 0; y < blockIndex.GetLength(1); y++)
- {
- // blockIndex[x, y]中保存的是分塊的顯示次序,能夠將其轉換爲對應的座標
- RectangleF rect = new RectangleF(((blockIndex[x, y] - 1) % blockIndex.GetLength(0)) * blockSize,
- ((blockIndex[x, y] - 1) / blockIndex.GetLength(0)) * 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 對角閉幕
- // 原理:每次繪製全部圖像內容,而後將不可見的區域生成閉合GraphicsPath並用背景色填充該區域
- private void Animator15()
- {
- const int stepCount = 4; // y軸的增量像素,應能被高度整除
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
- // 左上角座標p0(x0, y0)從左到右,p1(x1, y1)從上到下
- // 右下角座標p2(x2, y2)從右到左,p3(x3, y3)從下到上
- // 這四個點與左下角點和右上角點構成不可見區域
- PointF p0 = new Point(0, 0);
- PointF p1 = new Point(0, 0);
- PointF p2 = new Point(bmp.Width - 1, bmp.Height - 1);
- PointF p3 = new Point(bmp.Width - 1, bmp.Height - 1);
- // 表示不可見區域的閉合路徑
- GraphicsPath path = new GraphicsPath();
- // 以y軸stepCount個像素爲增量,也可使用x軸爲增量,通常使用較短的那個軸
- for (int y = 0; y < bmp.Height; y += stepCount)
- {
- p0.X = y * Convert.ToSingle(bmp.Width) / Convert.ToSingle(bmp.Height); // 以浮點數計算,保證精度
- p1.Y = y;
- p2.X = bmp.Width - 1 - p0.X;
- p3.Y = bmp.Height - 1 - p1.Y;
- path.Reset();
- path.AddPolygon(new PointF[] { p0, new PointF(bmp.Width, 0), p3, p2, new PointF(0, bmp.Height), p1 });
- dc.DrawImage(bmp, 0, 0); // 繪製所有圖像
- dc.FillPath(new SolidBrush(Color.FromKnownColor(KnownColor.ButtonFace)), path); // 填充不可見區域
- ShowBmp(path.GetBounds());
- Thread.Sleep(10 * delay);
- }
- // 因爲最後一次繪製的不可見區域並不須要,在這裏使所有區域可見
- dc.DrawImage(bmp, 0, 0);
- ShowBmp();
- }
- catch (Exception ex)
- {
- ShowError(ex.Message);
- }
- finally
- {
- OnDrawCompleted(this, EventArgs.Empty);
- }
- }
- #endregion
- #region 垂直百葉(改進版)
- // 原理:在圖像的垂直方向分爲高度相等的若干條,而後從上到下計算每次須要顯示的區域
- private void Animator16()
- {
- const float lineHeight = 50; // 百葉高度
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
- GraphicsPath path = new GraphicsPath();
- TextureBrush textureBrush = new TextureBrush(bmp);
- for (int i = 0; i < lineHeight; i++) // 每條百葉逐行像素顯示
- {
- for (int j = 0; j < Math.Ceiling(bmp.Height / lineHeight); j++)
- {
- RectangleF rect = new RectangleF(0, lineHeight * j + i, bmp.Width, 1);
- path.AddRectangle(rect);
- }
- dc.FillPath(textureBrush, path);
- ShowBmp();
- Thread.Sleep(10 * delay);
- }
- }
- catch (Exception ex)
- {
- ShowError(ex.Message);
- }
- finally
- {
- OnDrawCompleted(this, EventArgs.Empty);
- }
- }
- #endregion
- #region 壓縮豎條(改進版)
- // 原理:在圖像的水平方向分爲寬度相等的若干條,而後逐一加寬每條豎條的寬度,並在其中顯示該條圖像的所有內容
- private void Animator17()
- {
- const float lineWidth = 100; // 分條寬度
- const int stepCount = 4; // 每次加寬的步進像素,應能被lineWidth整除
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
- for (int i = 0; i < Math.Ceiling(bmp.Width / lineWidth); i++)
- {
- for (int j = stepCount; j <= lineWidth; j += stepCount) // 每條寬度逐漸增長,以產生縮放效果
- {
- RectangleF sourRect = new RectangleF(lineWidth * i, 0, lineWidth, bmp.Height);
- RectangleF destRect = new RectangleF(lineWidth * i, 0, j, bmp.Height);
- 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 Animator18()
- {
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- //ClearBackground();
- for (float i = 1; i <= dc.DpiX; i++)
- {
- RectangleF destRect = new RectangleF(0, 0, bmp.Width * dc.DpiX / i, bmp.Height);
- RectangleF sourRect = new RectangleF(0, 0, bmp.Width, bmp.Height);
- dc.DrawImage(bmp, destRect, sourRect, GraphicsUnit.Pixel);
- ShowBmp();
- Thread.Sleep(10 * delay);
- }
- }
- catch (Exception ex)
- {
- ShowError(ex.Message);
- }
- finally
- {
- OnDrawCompleted(this, EventArgs.Empty);
- }
- }
- #endregion
- #region 三色對接(改進版)
- // 原理:使用ImageAttributes類和顏色轉換矩陣處理圖像,首先R和B分別從左右向中心移動,相遇後繼續移動,且G從兩側向中間移動,直到相遇
- private void Animator19()
- {
- const int stepCount = 4; // 各個區域每次增長的像素量
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
- // 創建三個時間段所需的5個不一樣顏色轉換後的位圖
- Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); // 位圖的所有矩形區域
- // 紅色份量位圖
- ColorMatrix matrix = new ColorMatrix();
- matrix.Matrix00 = 1f; // R
- matrix.Matrix11 = 0f; // G
- matrix.Matrix22 = 0f; // B
- ImageAttributes attributes = new ImageAttributes();
- attributes.SetColorMatrix(matrix); // 使用R份量轉換矩陣
- Bitmap redBmp = new Bitmap(bmp.Width, bmp.Height);
- Graphics.FromImage(redBmp).DrawImage(bmp, rect, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
- // 藍色份量位圖
- matrix.Matrix00 = 0f; // R
- matrix.Matrix11 = 0f; // G
- matrix.Matrix22 = 1f; // B
- attributes.SetColorMatrix(matrix); // 使用B份量轉換矩陣
- Bitmap blueBmp = new Bitmap(bmp.Width, bmp.Height);
- Graphics.FromImage(blueBmp).DrawImage(bmp, rect, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
- // 紅藍份量位圖
- matrix.Matrix00 = 1f; // R
- matrix.Matrix11 = 0f; // G
- matrix.Matrix22 = 1f; // B
- attributes.SetColorMatrix(matrix); // 使用B份量轉換矩陣
- Bitmap redBlueBmp = new Bitmap(bmp.Width, bmp.Height);
- Graphics.FromImage(redBlueBmp).DrawImage(bmp, rect, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
- // 紅綠份量位圖
- matrix.Matrix00 = 1f; // R
- matrix.Matrix11 = 1f; // G
- matrix.Matrix22 = 0f; // B
- attributes.SetColorMatrix(matrix); // 使用B份量轉換矩陣
- Bitmap redGreenBmp = new Bitmap(bmp.Width, bmp.Height);
- Graphics.FromImage(redGreenBmp).DrawImage(bmp, rect, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
- // 藍綠份量位圖
- matrix.Matrix00 = 0f; // R
- matrix.Matrix11 = 1f; // G
- matrix.Matrix22 = 1f; // B
- attributes.SetColorMatrix(matrix); // 使用B份量轉換矩陣
- Bitmap blueGreenBmp = new Bitmap(bmp.Width, bmp.Height);
- Graphics.FromImage(blueGreenBmp).DrawImage(bmp, rect, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
- // 第1段:1/2時間(設從左到右時間爲1),R和B分別從左右向中間移動,在1/2處相遇
- for (int x = 0; x < bmp.Width / 2; x += stepCount)
- {
- Rectangle rectR = new Rectangle(x, 0, stepCount, bmp.Height); // R的區域,從左到右
- dc.DrawImage(redBmp, rectR, rectR, GraphicsUnit.Pixel);
- Rectangle rectB = new Rectangle(bmp.Width - x - stepCount, 0, stepCount, bmp.Height); // B的區域,從右到左
- dc.DrawImage(blueBmp, rectB, rectB, GraphicsUnit.Pixel);
- ShowBmp(Rectangle.Union(rectR, rectB));
- Thread.Sleep(10 * delay);
- }
- // 第2段:1/4時間,R和B從中間分別向右、左移動,G從左右向中間移動,在1/4和3/4處相遇
- ColorMatrix matrixGLeft = new ColorMatrix(); // 處理從左到右的G
- ColorMatrix matrixGRight = new ColorMatrix(); // 處理從右到左的G
- for (int x = 0; x < bmp.Width / 4; x += stepCount)
- {
- Rectangle rectBR = new Rectangle(bmp.Width / 2 - x - stepCount, 0, 2 * (x + stepCount), bmp.Height); // B和R的區域(位於中心)
- dc.DrawImage(redBlueBmp, rectBR, rectBR, GraphicsUnit.Pixel);
- Rectangle rectGLeft = new Rectangle(x, 0, stepCount, bmp.Height); // 左側G的區域,從左到右
- dc.DrawImage(redGreenBmp, rectGLeft, rectGLeft, GraphicsUnit.Pixel);
- Rectangle rectGRight = new Rectangle(bmp.Width - x - stepCount, 0, stepCount, bmp.Height); // 右側G的區域,從右到左
- dc.DrawImage(blueGreenBmp, rectGRight, rectGRight, GraphicsUnit.Pixel);
- ShowBmp(Rectangle.Union(Rectangle.Union(rectBR, rectGLeft), rectGRight));
- Thread.Sleep(10 * delay);
- }
- // 第3段:1/4時間,顯示全色,在1/4處同時向左右兩側擴展(即每次左右各擴展stepCount像素),3/4處與1/4處相同
- for (int x = 0; x < bmp.Width / 4; x += stepCount)
- {
- Rectangle rect1_4 = new Rectangle(bmp.Width / 4 - x - stepCount, 0, 2 * (x + stepCount), bmp.Height); // 1/4處的區域
- dc.DrawImage(bmp, rect1_4, rect1_4, GraphicsUnit.Pixel);
- Rectangle rect3_4 = new Rectangle(bmp.Width / 4 * 3 - x - stepCount, 0, 2 * (x + stepCount), bmp.Height); // 3/4處的區域
- dc.DrawImage(bmp, rect3_4, rect3_4, GraphicsUnit.Pixel);
- ShowBmp(Rectangle.Union(rect1_4, rect3_4));
- Thread.Sleep(10 * delay);
- }
- }
- catch (Exception ex)
- {
- ShowError(ex.Message);
- }
- finally
- {
- OnDrawCompleted(this, EventArgs.Empty);
- }
- }
- #endregion
- #region 對角滑動(改進版)
- // 原理:在水平方向從右到左,在垂直方向從下到上移動圖像
- private void Animator20()
- {
- const int movePixel = 4; // 每次移動的像素,應能被圖像高度整除
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
- RectangleF sourRect = new RectangleF(0, 0, bmp.Width, bmp.Height);
- for (int y = bmp.Height; y >= 0; y -= movePixel) // 從下到上移動圖像
- {
- // 按比例計算水平方向移動的量
- RectangleF destRect = new RectangleF(y * Convert.ToSingle(bmp.Width) / bmp.Height, y, bmp.Width, bmp.Height);
- 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
源程序沒有必要解釋了,當初編寫的時候我就加了很是詳細的註釋,只要你又必定的.NET基礎,應該徹底能夠讀懂!
這裏是本文的最後一部分:http://mengliao.blog.51cto.com/876134/473214windows