原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://mengliao.blog.51cto.com/876134/473169
这是一个比较复杂的程序,包含了30种图像动画特效演示,使用C#编写,源程序大概2000多行。 这个软件实际上主要是四个方面的内容: 1、30种动画特效算法,包含诸如随机拉丝、交替分块、多经扫描等等。这些算法设计的比较巧妙,也就是说大量的使用了图像处理的一些技巧。 2、.NET的GDI+技术功能非常强大,本软件中几乎涉及了GDI+中的各个方面,例如仿射变换矩阵、颜色变换矩阵、块处理等等。 3、采用多线程技术,使得软件看起来比较有序,同时采用信号量来实现暂停、继续、取消等功能。 4、采用比较严谨的面向对象的程序设计技术,从类的定义到方法的、事件的定义都严格按照OOP理论完成,可以说比较完整、精确的体现了OOP精髓。 这是截屏动画效果:
由于源程序太多,所以分三次发出,这次是(上),先发前10个动画特效:
- 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);
-
-
- private Graphics dc;
-
- #endregion
-
- #region 属性和事件
-
- private Bitmap outBmp;
-
-
-
- public Bitmap OutBmp
- {
- get { return outBmp; }
- }
-
- private int delay;
-
-
-
- public int Delay
- {
- get { return delay; }
- set { delay = Math.Min(Math.Max(1, value), 100); }
- }
-
-
-
-
- public event PaintEventHandler Redraw;
- protected void OnRedraw(Rectangle clipRectangle)
- {
- if (Redraw != null)
- {
- Redraw.Invoke(this, new PaintEventArgs(dc, clipRectangle));
- }
- }
-
-
-
-
- public event EventHandler DrawStarted;
- protected void OnDrawStarted(object sender, EventArgs e)
- {
- drawStarted = true;
- if (DrawStarted != null)
- {
- DrawStarted.Invoke(sender, e);
- }
- }
-
-
-
-
- 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 动画控制
-
-
-
-
- public void CancelDraw()
- {
- if (drawStarted)
- {
- cancelEvent.Set();
- }
- }
-
-
-
-
- public void PauseDraw()
- {
- if (drawStarted)
- {
- pauseEvent.Set();
- }
- }
-
-
-
-
- public void ResumeDraw()
- {
- if (drawStarted)
- {
- pauseEvent.Reset();
- }
- }
-
- #endregion
-
- #region 构造函数
-
-
-
-
- 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 绘制动画
-
-
-
-
- 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);
-
-
- 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);
- 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 = 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);
- }
-
- 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 = 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);
- }
- }
- 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();
- 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 透明淡入(改进版)
-
-
- private void Animator08()
- {
- const float stepCount = 0.02f;
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
-
-
- ImageAttributes attributes = new ImageAttributes();
-
- ColorMatrix matrix = new ColorMatrix();
- float value = 0;
- while (value < 1f)
- {
- matrix.Matrix33 = value;
-
-
- attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
- dc.Clear(Color.FromKnownColor(KnownColor.ButtonFace));
- 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 Animator09()
- {
- const float stepCount = 0.025f;
- try
- {
- OnDrawStarted(this, EventArgs.Empty);
- ClearBackground();
-
-
- ImageAttributes attributes = new ImageAttributes();
-
- ColorMatrix matrix = new ColorMatrix();
- matrix.Matrix00 = 0f;
- matrix.Matrix11 = 0f;
- matrix.Matrix22 = 0f;
-
-
-
- float value = 0f;
- while (value < 1f)
- {
- matrix.Matrix22 = value;
-
-
- 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;
- 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;
- 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
本文出自 “梦辽软件工作室” 博客,请务必保留此出处http://mengliao.blog.51cto.com/876134/473169
|