30种图像动画特效算法(C#多线程版)(上)

简介:

   这是一个比较复杂的程序,包含了30种图像动画特效演示,使用C#编写,源程序大概2000多行。
    这个软件实际上主要是四个方面的内容:
    1、30种动画特效算法,包含诸如随机拉丝、交替分块、多经扫描等等。这些算法设计的比较巧妙,也就是说大量的使用了图像处理的一些技巧。
    2、.NET的GDI+技术功能非常强大,本软件中几乎涉及了GDI+中的各个方面,例如仿射变换矩阵、颜色变换矩阵、块处理等等。
    3、采用多线程技术,使得软件看起来比较有序,同时采用信号量来实现暂停、继续、取消等功能。
    4、采用比较严谨的面向对象的程序设计技术,从类的定义到方法的、事件的定义都严格按照OOP理论完成,可以说比较完整、精确的体现了OOP精髓。
    这是截屏动画效果:
 
    由于源程序太多,所以分三次发出,这次是(上),先发前10个动画特效: 

 
 
  1. using System;  
  2. using System.Drawing;  
  3. using System.Drawing.Drawing2D;  
  4. using System.Drawing.Imaging;  
  5. using System.Threading;  
  6. using System.Windows.Forms;  
  7.  
  8. namespace Mengliao.CSharp.A14  
  9. {  
  10.     #region  动画类型枚举  
  11.  
  12.     // 本例为了简单起见没有使用见名知意的名称,如用于实际可将这些枚举成员更名为准确的英文名称  
  13.     enum AnimateType  
  14.     {  
  15.         Animator01, Animator02, Animator03, Animator04, Animator05,  
  16.         Animator06, Animator07, Animator08, Animator09, Animator10,  
  17.         Animator11, Animator12, Animator13, Animator14, Animator15,  
  18.         Animator16, Animator17, Animator18, Animator19, Animator20,  
  19.         Animator21, Animator22, Animator23, Animator24, Animator25,  
  20.         Animator26, Animator27, Animator28, Animator29, Animator30  
  21.     }  
  22.  
  23.     #endregion  
  24.  
  25.     class AnimatorImage  
  26.     {  
  27.         #region 私有字段  
  28.  
  29.         // 输入位图  
  30.         private Bitmap bmp;  
  31.         // 是否已经开始绘制  
  32.         private bool drawStarted = false;  
  33.  
  34.         // 在绘制过程中是否终止的自动复位信号量,有信号则终止  
  35.         private AutoResetEvent cancelEvent = new AutoResetEvent(false);  
  36.  
  37.         // 在绘制过程中是否暂停的手动复位信号量,有信号则暂停  
  38.         private ManualResetEvent pauseEvent = new ManualResetEvent(false);  
  39.  
  40.         // 输出位图的DC  
  41.         private Graphics dc;  
  42.  
  43.         #endregion  
  44.  
  45.         #region 属性和事件  
  46.  
  47.         private Bitmap outBmp;  
  48.         /// <summary>  
  49.         /// 输出位图。  
  50.         /// </summary>  
  51.         public Bitmap OutBmp  
  52.         {  
  53.             get { return outBmp; }  
  54.         }  
  55.  
  56.         private int delay;  
  57.         /// <summary>  
  58.         /// 延时系数。  
  59.         /// </summary>  
  60.         public int Delay  
  61.         {  
  62.             get { return delay; }  
  63.             set { delay = Math.Min(Math.Max(1, value), 100); } // 使其介于1到100之间  
  64.         }  
  65.  
  66.         /// <summary>  
  67.         /// 重绘事件。  
  68.         /// </summary>  
  69.         public event PaintEventHandler Redraw;  
  70.         protected void OnRedraw(Rectangle clipRectangle)  
  71.         {  
  72.             if (Redraw != null)  
  73.             {  
  74.                 Redraw.Invoke(thisnew PaintEventArgs(dc, clipRectangle));  
  75.             }  
  76.         }  
  77.  
  78.         /// <summary>  
  79.         /// 绘制开始事件。  
  80.         /// </summary>  
  81.         public event EventHandler DrawStarted;  
  82.         protected void OnDrawStarted(object sender, EventArgs e)  
  83.         {  
  84.             drawStarted = true;  
  85.             if (DrawStarted != null)  
  86.             {  
  87.                 DrawStarted.Invoke(sender, e);  
  88.             }  
  89.         }  
  90.  
  91.         /// <summary>  
  92.         /// 绘制完成事件。  
  93.         /// </summary>  
  94.         public event EventHandler DrawCompleted;  
  95.         protected void OnDrawCompleted(object sender, EventArgs e)  
  96.         {  
  97.             drawStarted = false;  
  98.             cancelEvent.Reset();  
  99.             if (DrawCompleted != null)  
  100.             {  
  101.                 DrawCompleted.Invoke(sender, e);  
  102.             }  
  103.         }  
  104.  
  105.         #endregion  
  106.  
  107.         #region 私有方法  
  108.  
  109.         // 在输出位图上显示绘制过程中的错误信息  
  110.         private void ShowError(string errMsg)  
  111.         {  
  112.             Font font = new Font("宋体", 9);  
  113.             SizeF size = dc.MeasureString(errMsg, font);  
  114.             PointF point = new PointF((outBmp.Width - size.Width) / 2f, (outBmp.Height - size.Height) / 2f);  
  115.             // 在文字的四个方向各一个像素处绘制其它颜色的文字,以形成边框,否则可能看不清除文字  
  116.             dc.DrawString(errMsg, font, Brushes.Red, point.X - 1f, point.Y);  
  117.             dc.DrawString(errMsg, font, Brushes.Red, point.X + 1f, point.Y);  
  118.             dc.DrawString(errMsg, font, Brushes.Red, point.X, point.Y - 1f);  
  119.             dc.DrawString(errMsg, font, Brushes.Red, point.X, point.Y + 1f);  
  120.             // 绘制文字  
  121.             dc.DrawString(errMsg, font, Brushes.White, point);  
  122.             ShowBmp(new Rectangle(Point.Round(point), Size.Round(size)));  
  123.         }  
  124.  
  125.         // 供绘制动画方法内部调用,三个重载版本  
  126.         private void ShowBmp(Rectangle clipRectangle)  
  127.         {  
  128.             string cancelMsg = "绘图操作已被用户取消!";  
  129.             OnRedraw(clipRectangle);  
  130.             if (cancelEvent.WaitOne(0)) // 取消  
  131.             {  
  132.                 // 该异常将被外部方法捕获,即各个绘制方法  
  133.                 throw new ApplicationException(cancelMsg);  
  134.             }  
  135.             while (pauseEvent.WaitOne(0)) // 暂停  
  136.             {  
  137.                 if (cancelEvent.WaitOne(10)) // 在暂停期间取消  
  138.                 {  
  139.                     pauseEvent.Reset();  
  140.                     throw new ApplicationException(cancelMsg);  
  141.                 }  
  142.             }  
  143.         }  
  144.         private void ShowBmp(RectangleF clipRectangle) // 接收浮点参数  
  145.         {  
  146.             ShowBmp(Rectangle.Round(clipRectangle));  
  147.         }  
  148.         private void ShowBmp() // 重绘全部区域  
  149.         {  
  150.             ShowBmp(new Rectangle(0, 0, bmp.Width, bmp.Height));  
  151.         }  
  152.  
  153.         // 清空背景  
  154.         private void ClearBackground()  
  155.         {  
  156.             dc.Clear(Color.FromKnownColor(KnownColor.ButtonFace)); // 置背景色  
  157.             ShowBmp(); // 重绘所有区域  
  158.         }  
  159.  
  160.         #endregion  
  161.  
  162.         #region 动画控制  
  163.  
  164.         /// <summary>  
  165.         /// 取消绘制。  
  166.         /// </summary>  
  167.         public void CancelDraw()  
  168.         {  
  169.             if (drawStarted)  
  170.             {  
  171.                 cancelEvent.Set();  
  172.             }  
  173.         }  
  174.  
  175.         /// <summary>  
  176.         /// 暂停绘制。  
  177.         /// </summary>  
  178.         public void PauseDraw()  
  179.         {  
  180.             if (drawStarted)  
  181.             {  
  182.                 pauseEvent.Set();  
  183.             }  
  184.         }  
  185.  
  186.         /// <summary>  
  187.         /// 继续绘制。  
  188.         /// </summary>  
  189.         public void ResumeDraw()  
  190.         {  
  191.             if (drawStarted)  
  192.             {  
  193.                 pauseEvent.Reset();  
  194.             }  
  195.         }  
  196.  
  197.         #endregion  
  198.  
  199.         #region 构造函数  
  200.         /// <summary>  
  201.         /// 实例化后,需分配事件处理方法,所有事件均在独立线程中触发;默认延时系数为1。  
  202.         /// </summary>  
  203.         /// <param name="inBmp">输入位图</param>  
  204.         public AnimatorImage(Bitmap inBmp)  
  205.         {  
  206.             delay = 1;  
  207.             this.bmp = (Bitmap)inBmp.Clone();  
  208.             outBmp = new Bitmap(this.bmp.Width, this.bmp.Height);  
  209.             dc = Graphics.FromImage(outBmp);  
  210.         }  
  211.  
  212.         #endregion  
  213.  
  214.         #region 绘制动画  
  215.         /// <summary>  
  216.         /// 以独立线程的方式开始显示动画。  
  217.         /// </summary>  
  218.         /// <param name="animateType">动画类型枚举</param>  
  219.         public void DrawAnimator(AnimateType animateType)  
  220.         {  
  221.             if (drawStarted) // 判断动画是否已经开始绘制  
  222.             {  
  223.                 if (pauseEvent.WaitOne(0)) // 动画已开始,但被暂停了,继续  
  224.                     pauseEvent.Reset();  
  225.                 else 
  226.                     pauseEvent.Set();  
  227.                 return;  
  228.             }  
  229.             ThreadStart threadMethod;  
  230.             switch (animateType)  
  231.             {  
  232.                 case AnimateType.Animator01:  
  233.                     threadMethod = Animator01;  
  234.                     break;  
  235.                 case AnimateType.Animator02:  
  236.                     threadMethod = Animator02;  
  237.                     break;  
  238.                 case AnimateType.Animator03:  
  239.                     threadMethod = Animator03;  
  240.                     break;  
  241.                 case AnimateType.Animator04:  
  242.                     threadMethod = Animator04;  
  243.                     break;  
  244.                 case AnimateType.Animator05:  
  245.                     threadMethod = Animator05;  
  246.                     break;  
  247.                 case AnimateType.Animator06:  
  248.                     threadMethod = Animator06;  
  249.                     break;  
  250.                 case AnimateType.Animator07:  
  251.                     threadMethod = Animator07;  
  252.                     break;  
  253.                 case AnimateType.Animator08:  
  254.                     threadMethod = Animator08;  
  255.                     break;  
  256.                 case AnimateType.Animator09:  
  257.                     threadMethod = Animator09;  
  258.                     break;  
  259.                 case AnimateType.Animator10:  
  260.                     threadMethod = Animator10;  
  261.                     break;  
  262.                 case AnimateType.Animator11:  
  263.                     threadMethod = Animator11;  
  264.                     break;  
  265.                 case AnimateType.Animator12:  
  266.                     threadMethod = Animator12;  
  267.                     break;  
  268.                 case AnimateType.Animator13:  
  269.                     threadMethod = Animator13;  
  270.                     break;  
  271.                 case AnimateType.Animator14:  
  272.                     threadMethod = Animator14;  
  273.                     break;  
  274.                 case AnimateType.Animator15:  
  275.                     threadMethod = Animator15;  
  276.                     break;  
  277.                 case AnimateType.Animator16:  
  278.                     threadMethod = Animator16;  
  279.                     break;  
  280.                 case AnimateType.Animator17:  
  281.                     threadMethod = Animator17;  
  282.                     break;  
  283.                 case AnimateType.Animator18:  
  284.                     threadMethod = Animator18;  
  285.                     break;  
  286.                 case AnimateType.Animator19:  
  287.                     threadMethod = Animator19;  
  288.                     break;  
  289.                 case AnimateType.Animator20:  
  290.                     threadMethod = Animator20;  
  291.                     break;  
  292.                 case AnimateType.Animator21:  
  293.                     threadMethod = Animator21;  
  294.                     break;  
  295.                 case AnimateType.Animator22:  
  296.                     threadMethod = Animator22;  
  297.                     break;  
  298.                 case AnimateType.Animator23:  
  299.                     threadMethod = Animator23;  
  300.                     break;  
  301.                 case AnimateType.Animator24:  
  302.                     threadMethod = Animator24;  
  303.                     break;  
  304.                 case AnimateType.Animator25:  
  305.                     threadMethod = Animator25;  
  306.                     break;  
  307.                 case AnimateType.Animator26:  
  308.                     threadMethod = Animator26;  
  309.                     break;  
  310.                 case AnimateType.Animator27:  
  311.                     threadMethod = Animator27;  
  312.                     break;  
  313.                 case AnimateType.Animator28:  
  314.                     threadMethod = Animator28;  
  315.                     break;  
  316.                 case AnimateType.Animator29:  
  317.                     threadMethod = Animator29;  
  318.                     break;  
  319.                 default:  
  320.                     threadMethod = Animator30;  
  321.                     break;  
  322.             }  
  323.             Thread drawThread = new Thread(threadMethod);  
  324.             drawThread.IsBackground = true// 设为后台线程,避免该线程未结束时退出主线程而引发异常  
  325.             drawThread.Start();  
  326.         }  
  327.  
  328.         #endregion  
  329.  
  330.         // ==========  
  331.         // 动画算法  
  332.         // ==========  
  333.  
  334.         #region 压缩反转(改进版)  
  335.  
  336.         // 原理:计算图像位置和高度,以高度的一半为轴进行对换上下半边的图像  
  337.         private void Animator01()  
  338.         {  
  339.             const float blockSize = 8; // 每次显示的高度增量,应能被高度整除  
  340.             try 
  341.             {  
  342.                 OnDrawStarted(this, EventArgs.Empty); // 触发开始绘制事件  
  343.                 //ClearBackground();  
  344.  
  345.                 Color bgColor = Color.FromKnownColor(KnownColor.ButtonFace);  
  346.                 RectangleF srcRect = new RectangleF(0, 0, bmp.Width, bmp.Height);  
  347.                 for (float i = (float)Math.Floor(-bmp.Height / blockSize); i <= Math.Ceiling(bmp.Height / blockSize); i++)  
  348.                 {  
  349.                     dc.Clear(bgColor); // 清空DC  
  350.                     float j = i * blockSize / 2;  
  351.                     float destTop = bmp.Height / 2 - j; // 目标矩形的顶位置  
  352.                     // 目标矩形区域在循环的前半段为垂直反向  
  353.                     RectangleF destRect = new RectangleF(0, destTop, bmp.Width, 2 * j);  
  354.                     // 在指定区域绘制图像,该图像被拉伸  
  355.                     dc.DrawImage(bmp, destRect, srcRect, GraphicsUnit.Pixel);  
  356.  
  357.                     ShowBmp();  
  358.                     Thread.Sleep(10 * delay); // 休眠  
  359.                 }  
  360.             }  
  361.             catch (Exception ex)  
  362.             {  
  363.                 ShowError(ex.Message);  
  364.             }  
  365.             finally 
  366.             {  
  367.                 OnDrawCompleted(this, EventArgs.Empty); // 触发完成绘制事件  
  368.             }  
  369.         }  
  370.  
  371.         #endregion  
  372.  
  373.         #region 垂直对接(改进版)  
  374.  
  375.         // 原理:将图像分为上下部分,然后同时向中心移动  
  376.         private void Animator02()  
  377.         {  
  378.             const int stepCount = 4; // 每次上下移动的步长像素,应能被高度整除  
  379.             try 
  380.             {  
  381.                 OnDrawStarted(this, EventArgs.Empty);  
  382.                 ClearBackground();  
  383.  
  384.                 Rectangle sourTopRect = new Rectangle(0, 0, bmp.Width, bmp.Height / 2); // 上半部分源区域  
  385.                 Rectangle sourBottRect = new Rectangle(0, bmp.Height / 2, bmp.Width, bmp.Height / 2); // 下半部分源区域  
  386.                 for (int i = 0; i <= bmp.Height / 2; i += stepCount)  
  387.                 {  
  388.                     Rectangle destTopRect = new Rectangle(0, i - bmp.Height / 2 + 1, bmp.Width, bmp.Height / 2); // 上半部分目标区域  
  389.                     Rectangle destBottRect = new Rectangle(0, bmp.Height - i - 1, bmp.Width, bmp.Height / 2); // 下半部分目标区域  
  390.                     dc.DrawImage(bmp, destTopRect, sourTopRect, GraphicsUnit.Pixel);  
  391.                     dc.DrawImage(bmp, destBottRect, sourBottRect, GraphicsUnit.Pixel);  
  392.  
  393.                     ShowBmp(Rectangle.Union(destTopRect, destBottRect));  
  394.                     Thread.Sleep(10 * delay);  
  395.                 }  
  396.             }  
  397.             catch (Exception ex)  
  398.             {  
  399.                 ShowError(ex.Message);  
  400.             }  
  401.             finally 
  402.             {  
  403.                 OnDrawCompleted(this, EventArgs.Empty);  
  404.             }  
  405.         }  
  406.  
  407.         #endregion  
  408.  
  409.         #region 中心闭幕(改进版)  
  410.  
  411.         // 原理:由大到小生成图像中心区域,然后用总区域减去该中心区域,并用材质画刷填充  
  412.         private void Animator03()  
  413.         {  
  414.             const float stepCount = 4; // 每次收缩的步长像素  
  415.             try 
  416.             {  
  417.                 OnDrawStarted(this, EventArgs.Empty);  
  418.                 ClearBackground();  
  419.  
  420.                 // 建立空区域,如使用Region的无参构造函数则建立一个无限大小的区域,而非空区域  
  421.                 Region region = new Region(new GraphicsPath());  
  422.                 // 建立位图材质画刷  
  423.                 TextureBrush textureBrush = new TextureBrush(bmp);  
  424.                 for (float x = 0; x <= bmp.Width / 2f; x += stepCount)  
  425.                 {  
  426.                     // 添加整个位图区域  
  427.                     region.Union(new Rectangle(0, 0, bmp.Width, bmp.Height));  
  428.                     // 从中心开始,由大到小填充背景色或填充缩小尺寸的原图  
  429.                     // 计算高度变化量,如果宽度大,则高度变化量小于宽度,否则大于宽度  
  430.                     float y = x * bmp.Height / bmp.Width;  
  431.                     RectangleF rect = new RectangleF(x, y, bmp.Width - 2f * x, bmp.Height - 2f * y);  
  432.                     // 计算整个位图区域与背景色区域的差集  
  433.                     region.Exclude(rect);  
  434.                     dc.FillRegion(textureBrush, region); // 使用材质画刷填充区域  
  435.  
  436.                     ShowBmp(region.GetBounds(dc));  
  437.                     Thread.Sleep(10 * delay);  
  438.                 }  
  439.                 // 由于stepCount可能无法被宽度整除,则最终生成的背景色区域并不为空,故在循环结束后绘制整个位图  
  440.                 dc.DrawImage(bmp, 0, 0);  
  441.  
  442.                 ShowBmp();  
  443.             }  
  444.             catch (Exception ex)  
  445.             {  
  446.                 ShowError(ex.Message);  
  447.             }  
  448.             finally 
  449.             {  
  450.                 OnDrawCompleted(this, EventArgs.Empty);  
  451.             }  
  452.         }  
  453.  
  454.         #endregion  
  455.  
  456.         #region 中心放大(改进版)  
  457.  
  458.         // 原理:由中心向边缘按高度和宽度的比例循环输出所有像素,直到高度和宽度为原始大小  
  459.         private void Animator04()  
  460.         {  
  461.             const int stepCount = 4; // 每次增加的像素量,应能被宽度整除  
  462.             try 
  463.             {  
  464.                 OnDrawStarted(this, EventArgs.Empty);  
  465.                 ClearBackground();  
  466.  
  467.                 Rectangle sourRect = new Rectangle(0, 0, bmp.Width, bmp.Height); // 源区域为整个位图  
  468.                 for (int i = 0; i <= bmp.Width / 2; i += stepCount)  
  469.                 {  
  470.                     int j = i * bmp.Height / bmp.Width; // 计算高度变化量,如果宽度大,则高度变化量小于宽度,否则大于宽度  
  471.                     Rectangle destRect = new Rectangle(bmp.Width / 2 - i, bmp.Height / 2 - j, 2 * i, 2 * j);  
  472.                     dc.DrawImage(bmp, destRect, sourRect, GraphicsUnit.Pixel);  
  473.  
  474.                     ShowBmp(destRect);  
  475.                     Thread.Sleep(10 * delay);  
  476.                 }  
  477.             }  
  478.             catch (Exception ex)  
  479.             {  
  480.                 ShowError(ex.Message);  
  481.             }  
  482.             finally 
  483.             {  
  484.                 OnDrawCompleted(this, EventArgs.Empty);  
  485.             }  
  486.         }  
  487.  
  488.         #endregion  
  489.  
  490.         #region 逐行分块  
  491.  
  492.         // 原理:将图像分为正方形块,然后从左到右,从上到下顺序显示  
  493.         private void Animator05()  
  494.         {  
  495.             const float blockSize = 50; // 正方块的边长  
  496.             try 
  497.             {  
  498.                 OnDrawStarted(this, EventArgs.Empty);  
  499.                 ClearBackground();  
  500.  
  501.                 // 防止最后一列、最后一行不足一块的尺寸而不显示,故采用上取整  
  502.                 for (int y = 0; y < Math.Ceiling(bmp.Height / blockSize); y++)  
  503.                 {  
  504.                     for (int x = 0; x < Math.Ceiling(bmp.Width / blockSize); x++)  
  505.                     {  
  506.                         RectangleF rect;  
  507.                         if (y % 2 == 0) // 从左到右  
  508.                         {  
  509.                             rect = new RectangleF(x * blockSize, y * blockSize, blockSize, blockSize);  
  510.                         }  
  511.                         else // 从右到左  
  512.                         {  
  513.                             rect = new RectangleF((bmp.Width / blockSize - x - 1) * blockSize, y * blockSize, blockSize, blockSize);  
  514.                         }  
  515.                         dc.DrawImage(bmp, rect, rect, GraphicsUnit.Pixel);  
  516.  
  517.                         ShowBmp(rect);  
  518.                         Thread.Sleep(10 * delay);  
  519.                     }  
  520.                 }  
  521.             }  
  522.             catch (Exception ex)  
  523.             {  
  524.                 ShowError(ex.Message);  
  525.             }  
  526.             finally 
  527.             {  
  528.                 OnDrawCompleted(this, EventArgs.Empty);  
  529.             }  
  530.         }  
  531.  
  532.         #endregion  
  533.  
  534.         #region 交替分块(改进版)  
  535.  
  536.         // 原理:将图像分为正方形块,然后计算所有分块按照奇偶从左到右显示或从右到左显示所需的区域,并用材质画刷填充  
  537.         private void Animator06()  
  538.         {  
  539.             const float blockSize = 70; // 正方块的边长  
  540.             const int showWidth = 1; // 每次显示的像素列数  
  541.             try 
  542.             {  
  543.                 OnDrawStarted(this, EventArgs.Empty);  
  544.                 ClearBackground();  
  545.  
  546.                 // 建立空区域,如使用Region的无参构造函数则建立一个无限大小的区域,而非空区域  
  547.                 Region region = new Region(new GraphicsPath());  
  548.                 // 建立位图材质画刷  
  549.                 TextureBrush textureBrush = new TextureBrush(bmp);  
  550.                 // 分块的行坐标+列坐标为偶数则从左到右逐列显示本块,否则从右到左逐列显示本块  
  551.                 for (int i = 0; i <= Math.Ceiling(blockSize / showWidth); i++)  
  552.                 {  
  553.                     for (int x = 0; x < Math.Ceiling(bmp.Width / blockSize); x++)  
  554.                     {  
  555.                         for (int y = 0; y < Math.Ceiling(bmp.Height / blockSize); y++)  
  556.                         {  
  557.                             RectangleF rect;  
  558.                             // 判断块的行列坐标和为奇数或偶数  
  559.                             if ((x + y) % 2 == 0)  
  560.                             {  
  561.                                 rect = new RectangleF(x * blockSize + i * showWidth, y * blockSize, showWidth, blockSize);  
  562.                             }  
  563.                             else 
  564.                             {  
  565.                                 rect = new RectangleF((x + 1) * blockSize - i * showWidth, y * blockSize, showWidth, blockSize);  
  566.                             }  
  567.                             region.Union(rect); // 将要显示的区域合并到region中  
  568.                         }  
  569.                     }  
  570.                     dc.FillRegion(textureBrush, region); // 使用材质画刷填充区域  
  571.  
  572.                     ShowBmp(region.GetBounds(dc));  
  573.                     Thread.Sleep(10 * delay);  
  574.                 }  
  575.             }  
  576.             catch (Exception ex)  
  577.             {  
  578.                 ShowError(ex.Message);  
  579.             }  
  580.             finally 
  581.             {  
  582.                 OnDrawCompleted(this, EventArgs.Empty);  
  583.             }  
  584.         }  
  585.  
  586.         #endregion  
  587.  
  588.         #region 交叉竖条(改进版)  
  589.  
  590.         // 原理:将图像分成宽度相等的列,然后计算从上下两个方向交叉前进的区域,并使用材质画刷填充  
  591.         private void Animator07()  
  592.         {  
  593.             const float lineWidth = 4; // 竖条宽度  
  594.             const float lineStep = 6; // 竖条每次前进的步长  
  595.             try 
  596.             {  
  597.                 OnDrawStarted(this, EventArgs.Empty);  
  598.                 ClearBackground();  
  599.  
  600.                 GraphicsPath path = new GraphicsPath(); // 建立路径,路径处理速度要明显快于Region,但不支持集合运算  
  601.                 TextureBrush textureBrush = new TextureBrush(bmp);  
  602.                 // 从上到下和从下到上以步长为单位显示  
  603.                 for (int y = 0; y < Math.Ceiling(bmp.Height / lineStep); y++)  
  604.                 {  
  605.                     // 显示两个方向的每个垂直竖条  
  606.                     for (int x = 0; x < Math.Ceiling(bmp.Width / lineWidth); x++)  
  607.                     {  
  608.                         RectangleF rect;  
  609.                         if (x % 2 == 0) // 从上到下  
  610.                         {  
  611.                             rect = new RectangleF(x * lineWidth, y * lineStep, lineWidth, lineStep);  
  612.                         }  
  613.                         else // 从下到上  
  614.                         {  
  615.                             rect = new RectangleF(x * lineWidth, bmp.Height - y * lineStep - lineStep, lineWidth, lineStep);  
  616.                         }  
  617.                         path.AddRectangle(rect);  
  618.                     }  
  619.                     dc.FillPath(textureBrush, path);  
  620.  
  621.                     ShowBmp(path.GetBounds());  
  622.                     Thread.Sleep(10 * delay);  
  623.                 }  
  624.             }  
  625.             catch (Exception ex)  
  626.             {  
  627.                 ShowError(ex.Message);  
  628.             }  
  629.             finally 
  630.             {  
  631.                 OnDrawCompleted(this, EventArgs.Empty);  
  632.             }  
  633.         }  
  634.  
  635.         #endregion  
  636.  
  637.         #region 透明淡入(改进版)  
  638.  
  639.         // 原理:使用ImageAttributes类和颜色转换矩阵处理图像,使每个像素的颜色分量同步增加  
  640.         private void Animator08()  
  641.         {  
  642.             const float stepCount = 0.02f; // 颜色转换矩阵增量,该值除1应等于整数  
  643.             try 
  644.             {  
  645.                 OnDrawStarted(this, EventArgs.Empty);  
  646.                 ClearBackground();  
  647.  
  648.                 // ImageAttributes类的实例用于调整颜色,由DrawImage()方法调用  
  649.                 ImageAttributes attributes = new ImageAttributes();  
  650.                 // 建立5*5阶RGBA颜色矩阵  
  651.                 ColorMatrix matrix = new ColorMatrix();  
  652.                 float value = 0;  
  653.                 while (value < 1f)  
  654.                 {  
  655.                     matrix.Matrix33 = value;  
  656.                     // 为ImageAttributes对象指定颜色调整矩阵  
  657.                     // ColorMatrixFlag.Default表示使用矩阵调整所有颜色;ColorAdjustType.Bitmap表示调整位图的颜色  
  658.                     attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);  
  659.                     dc.Clear(Color.FromKnownColor(KnownColor.ButtonFace)); // 清空DC,否则每次会将不同的透明度图像叠加显示  
  660.                     dc.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);  
  661.                     value += stepCount;  
  662.  
  663.                     ShowBmp();  
  664.                     Thread.Sleep(10 * delay);  
  665.                 }  
  666.             }  
  667.             catch (Exception ex)  
  668.             {  
  669.                 ShowError(ex.Message);  
  670.             }  
  671.             finally 
  672.             {  
  673.                 OnDrawCompleted(this, EventArgs.Empty);  
  674.             }  
  675.         }  
  676.  
  677.         #endregion  
  678.  
  679.         #region 三色淡入  
  680.  
  681.         // 原理:使用ImageAttributes类和颜色转换矩阵处理图像,分三次增加每个像素的单个颜色分量  
  682.         private void Animator09()  
  683.         {  
  684.             const float stepCount = 0.025f; // 颜色转换矩阵增量,该值除1应等于整数  
  685.             try 
  686.             {  
  687.                 OnDrawStarted(this, EventArgs.Empty);  
  688.                 ClearBackground();  
  689.  
  690.                 // ImageAttributes类的实例用于调整颜色,由DrawImage()方法调用  
  691.                 ImageAttributes attributes = new ImageAttributes();  
  692.                 // 建立5*5阶RGBA颜色矩阵  
  693.                 ColorMatrix matrix = new ColorMatrix();  
  694.                 matrix.Matrix00 = 0f; // R为0  
  695.                 matrix.Matrix11 = 0f; // G为0  
  696.                 matrix.Matrix22 = 0f; // B为0  
  697.                 // 以下三个循环依次处理B、R、G,符合亮度方程的原理  
  698.                 // 人眼对B最不敏感,对G最敏感,或者说B传达的亮度信息最少,G传达的亮度信息最多  
  699.                 // 因此先处理亮度信息少的,最后处理亮度信息多的,如果反过来处理,则变化不明显  
  700.                 float value = 0f;  
  701.                 while (value < 1f)  
  702.                 {  
  703.                     matrix.Matrix22 = value; // 颜色R的转换矩阵分量值  
  704.                     // 为ImageAttributes对象指定颜色调整矩阵  
  705.                     // ColorMatrixFlag.Default表示使用矩阵调整所有颜色;ColorAdjustType.Bitmap表示调整位图的颜色  
  706.                     attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);  
  707.                     dc.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);  
  708.                     value += stepCount;  
  709.  
  710.                     ShowBmp();  
  711.                     Thread.Sleep(10 * delay);  
  712.                 }  
  713.                 value = stepCount;  
  714.                 while (value < 1f)  
  715.                 {  
  716.                     matrix.Matrix00 = value; // 颜色G的转换矩阵分量值  
  717.                     attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);  
  718.                     dc.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);  
  719.                     value += stepCount;  
  720.  
  721.                     ShowBmp();  
  722.                     Thread.Sleep(10 * delay);  
  723.                 }  
  724.                 value = stepCount;  
  725.                 while (value < 1f)  
  726.                 {  
  727.                     matrix.Matrix11 = value; // 颜色B的转换矩阵分量值  
  728.                     attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);  
  729.                     dc.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);  
  730.                     value += stepCount;  
  731.  
  732.                     ShowBmp();  
  733.                     Thread.Sleep(10 * delay);  
  734.                 }  
  735.             }  
  736.             catch (Exception ex)  
  737.             {  
  738.                 ShowError(ex.Message);  
  739.             }  
  740.             finally 
  741.             {  
  742.                 OnDrawCompleted(this, EventArgs.Empty);  
  743.             }  
  744.         }  
  745.  
  746.         #endregion  
  747.  
  748.         #region 水平拉幕  
  749.  
  750.         // 原理:由中心向开始逐渐输出中心两侧的像素,直到宽度为原始大小  
  751.         private void Animator10()  
  752.         {  
  753.             const int stepCount = 4; // 每次增加的步长像素,该值应能被宽度整除  
  754.             try 
  755.             {  
  756.                 OnDrawStarted(this, EventArgs.Empty);  
  757.                 ClearBackground();  
  758.  
  759.                 for (int i = 0; i <= Math.Ceiling(bmp.Width / 2f); i += stepCount)  
  760.                 {  
  761.                     Rectangle rect = new Rectangle(bmp.Width / 2 - i, 0, 2 * i, bmp.Height);  
  762.                     dc.DrawImage(bmp, rect, rect, GraphicsUnit.Pixel);  
  763.  
  764.                     ShowBmp(rect);  
  765.                     Thread.Sleep(10 * delay);  
  766.                 }  
  767.             }  
  768.             catch (Exception ex)  
  769.             {  
  770.                 ShowError(ex.Message);  
  771.             }  
  772.             finally 
  773.             {  
  774.                 OnDrawCompleted(this, EventArgs.Empty);  
  775.             }  
  776.         }  
  777.  
  778.         #endregion 

    本文的最后一部分(下),会附上完整的项目文件组,包含所有资源及可执行文件。
这里是本文的第二部分:http://mengliao.blog.51cto.com/876134/473193










本文转自 BlackAlpha 51CTO博客,原文链接:http://blog.51cto.com/mengliao/473169,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
Java 调度 C#
C#学习系列相关之多线程(一)----常用多线程方法总结
C#学习系列相关之多线程(一)----常用多线程方法总结
|
1月前
|
安全 编译器 C#
C#学习相关系列之多线程---lock线程锁的用法
C#学习相关系列之多线程---lock线程锁的用法
|
1月前
|
C#
C#学习相关系列之多线程---ConfigureAwait的用法
C#学习相关系列之多线程---ConfigureAwait的用法
|
1月前
|
C#
C#学习相关系列之多线程---TaskCompletionSource用法(八)
C#学习相关系列之多线程---TaskCompletionSource用法(八)
|
1月前
|
Java C#
C#学习系列相关之多线程(五)----线程池ThreadPool用法
C#学习系列相关之多线程(五)----线程池ThreadPool用法
|
2天前
|
文字识别 算法 计算机视觉
图像倾斜校正算法的MATLAB实现:图像倾斜角检测及校正
图像倾斜校正算法的MATLAB实现:图像倾斜角检测及校正
12 0
|
1月前
|
C#
C#学习系列相关之多线程(二)----Thread类介绍
C#学习系列相关之多线程(二)----Thread类介绍
|
1月前
|
Java C#
C#学习相关系列之多线程(七)---Task的相关属性用法
C#学习相关系列之多线程(七)---Task的相关属性用法
|
1月前
|
Java C#
C#学习相关系列之多线程(六)----Task的初级使用
C#学习相关系列之多线程(六)----Task的初级使用
|
1月前
|
C#
C#学习系列相关之多线程(四)----async和await的用法
C#学习系列相关之多线程(四)----async和await的用法