Discuz教程网

用C#来实现以动画的方式显示图像

[复制链接]
authicon 炸酱面 发表于 2010-12-30 12:58:10 | 显示全部楼层 |阅读模式
说明:
由于是以动画方式显示图像,这里没办法直接贴静态截图,因此决定给大家代码开源,将所有的可运行代码附在案例后面,由于所有的动画处理图像的对象放在都pictureBox控件中,同时定义的类都大同小异,因此这里先把下面案例中要用到的所有类及装载图像的代码给大家,运行时用这里的代码加下面任意一个实例的代码即可运行程序!

  1.   private Bitmap SourceBitmap;
  2.   private Bitmap MyBitmap;
  3.   private void button2_Click(object sender, EventArgs e)
  4.   {
  5.   //打开图像文件
  6.   OpenFileDialog openFileDialog = new OpenFileDialog();
  7.   openFileDialog.Filter = "图像文件(JPeg, Gif, Bmp, etc.)
  8.   |*.jpg;*.jpeg;*.gif;*.bmp;*.tif; *.tiff; *.png|
  9. JPeg 图像文件(*.jpg;*.jpeg)
  10.   |*.jpg;*.jpeg |GIF 图像文件(*.gif)|*.gif |BMP图像文件(*.bmp)|*.bmp
  11.   |Tiff图像文件(*.tif;*.tiff)|*.tif;*.tiff|Png图像
  12. 文件(*.png)| *.png |所有文件(*.*)|*.*";
  13.   if (openFileDialog.ShowDialog() == DialogResult.OK)
  14.   {
  15.   //得到原始大小的图像
  16.   SourceBitmap = new Bitmap(openFileDialog.FileName);
  17.   //得到缩放后的图像
  18.   MyBitmap = new Bitmap(SourceBitmap, this.pictureBox1.Width, this
  19. .pictureBox1.Height);
  20.   this.pictureBox1.Image = MyBitmap;
  21.   }
  22.   }
复制代码
一、以上下反转的方式显示图像.
原理:计算图像位置和高度后以高度的一半为轴进行对换上下半边的图像。
代码
  1. private void button1_Click(object sender, EventArgs e)
  2. {

  3. try
  4. {
  5. int width = this.MyBitmap.Width; //图像宽度
  6. int height = this.MyBitmap.Height; //图像高度
  7. Graphics g = this.panel1.CreateGraphics();
  8. g.Clear(Color.Gray);
  9. for (int i = -width / 2; i <= width / 2; i++)
  10. {
  11. g.Clear(Color.Gray);
  12. int j = Convert.ToInt32(i * (Convert.ToSingle(height) / Convert.ToSingle(width)));
  13. Rectangle DestRect = new Rectangle(0, height / 2 -j, width, 2 * j);
  14. Rectangle SrcRect = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);
  15. g.DrawImage(MyBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
  16. System.Threading.Thread.Sleep(10);
  17. }
  18. }
  19. catch (Exception ex)
  20. {
  21. MessageBox.Show(ex.Message, "信息提示");
  22. }
  23. }
复制代码


二、以上下对接的方式显示图像
原理:首先将图像分为上下两部分, 然后分别显示。
代码:
  1. private void button1_Click(object sender, EventArgs e)
  2. {

  3. try
  4. {
  5. int width = this.pictureBox1.Width; //图像宽度
  6. int height = this.pictureBox1.Height; //图像高度
  7. Graphics g = this.panel1.CreateGraphics();
  8. g.Clear(Color.Gray);
  9. Bitmap bitmap = new Bitmap(width, height);
  10. int x = 0;
  11. while (x <= height / 2)
  12. {
  13. for (int i = 0; i <= width - 1; i++)
  14. {
  15. bitmap.SetPixel(i, x, MyBitmap.GetPixel(i, x));
  16. }
  17. for (int i = 0; i <= width - 1; i++)
  18. {
  19. bitmap.SetPixel(i, height - x - 1, MyBitmap.GetPixel(i, height - x - 1));
  20. }
  21. x++;
  22. this.panel1.Refresh();
  23. g.DrawImage (bitmap,0,0);
  24. System.Threading.Thread.Sleep(10);
  25. }
  26. }
  27. catch (Exception ex)
  28. {
  29. MessageBox.Show(ex.Message, "信息提示");
  30. }
  31. }
复制代码

三、以四周扩散的方式显示图像
原理:首先设置图像显示的位置, 然后按高度和宽度的比例循环输出, 直到高度和宽度为原始大小。
代码:
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. try
  4. {
  5. int width = this.MyBitmap.Width; //图像宽度
  6. int height = this.MyBitmap.Height; //图像高度
  7. //取得Graphics对象
  8. Graphics g = this.panel1.CreateGraphics();
  9. g.Clear(Color.Gray); //初始为全灰色
  10. for (int i = 0; i <= width / 2; i++)
  11. {
  12. int j = Convert.ToInt32 (i*(Convert.ToSingle(height) / Convert.ToSingle(width)));
  13. Rectangle DestRect = new Rectangle(width / 2 - i, height/2-j, 2 * i, 2*j);
  14. Rectangle SrcRect = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);
  15. g.DrawImage(MyBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
  16. System.Threading.Thread.Sleep(10);
  17. }
  18. }
  19. catch (Exception ex)
  20. {
  21. MessageBox.Show(ex.Message, "信息提示");
  22. }
  23. }
复制代码
四、以分块效果显示图像
原理:首先将图分为几块, 再使用 Bitmap 类的 Clone方法从原图指定的块中复制图像,
最后将这些块依次显示出来便可。
代码:
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. Graphics g = this.panel1.CreateGraphics();
  4. g.Clear(Color.White);
  5. int width = MyBitmap.Width;
  6. int height = MyBitmap.Height;
  7. //定义将图片切分成四个部分的区域
  8. RectangleF[] block ={
  9. new RectangleF(0,0,width/2,height/2), new RectangleF(width/2,0,width/2,height/2),
  10. new RectangleF(0,height/2,width/2,height/2),
  11. new RectangleF(width/2,height/2,width/2,height/2)};
  12. //分别克隆图片的四个部分
  13. Bitmap[] MyBitmapBlack ={
  14. MyBitmap.Clone(block[0],System.Drawing.Imaging.PixelFormat.DontCare),
  15. MyBitmap.Clone(block[1],System.Drawing.Imaging.PixelFormat.DontCare),
  16. MyBitmap.Clone(block[2],System.Drawing.Imaging.PixelFormat.DontCare),
  17. MyBitmap.Clone(block[3],System.Drawing.Imaging.PixelFormat.DontCare)};
  18. //绘制图片的四个部分,各部分绘制时间间隔为0.5秒
  19. g.DrawImage(MyBitmapBlack[0], 0, 0);
  20. System.Threading.Thread.Sleep(1000);
  21. g.DrawImage(MyBitmapBlack[1], width / 2, 0);
  22. System.Threading.Thread.Sleep(1000);
  23. g.DrawImage(MyBitmapBlack[3], width / 2, height / 2);
  24. System.Threading.Thread.Sleep(1000);
  25. g.DrawImage(MyBitmapBlack[2], 0, height / 2);
  26. }
复制代码
五、以淡入淡出效果显示图像
原理:使用 ImageAttrributes 类的 SetColorMatrix() 方法设置颜色, 调整矩阵实现淡出的效果. 此类还可以对颜色进行校正, 调暗, 调亮和移除等。
代码:

  1. private void button1_Click(object sender, EventArgs e)
  2. {

  3. try
  4. {
  5. Graphics g = this.panel1.CreateGraphics();
  6. g.Clear(Color.Gray);
  7. int width = MyBitmap.Width;
  8. int height = MyBitmap.Height;
  9. ImageAttributes attributes = new ImageAttributes();
  10. ColorMatrix matrix = new ColorMatrix();
  11. //创建淡入颜色矩阵
  12. matrix.Matrix00 = (float)0.0;
  13. matrix.Matrix01 = (float)0.0;
  14. matrix.Matrix02 = (float)0.0;
  15. matrix.Matrix03 = (float)0.0;
  16. matrix.Matrix04 = (float)0.0;
  17. matrix.Matrix10 = (float)0.0;
  18. matrix.Matrix11 = (float)0.0;
  19. matrix.Matrix12 = (float)0.0;
  20. matrix.Matrix13 = (float)0.0;
  21. matrix.Matrix14 = (float)0.0;
  22. matrix.Matrix20 = (float)0.0;
  23. matrix.Matrix21 = (float)0.0;
  24. matrix.Matrix22 = (float)0.0;
  25. matrix.Matrix23 = (float)0.0;
  26. matrix.Matrix24 = (float)0.0;
  27. matrix.Matrix30 = (float)0.0;
  28. matrix.Matrix31 = (float)0.0;
  29. matrix.Matrix32 = (float)0.0;
  30. matrix.Matrix33 = (float)0.0;
  31. matrix.Matrix34 = (float)0.0;
  32. matrix.Matrix40 = (float)0.0;
  33. matrix.Matrix41 = (float)0.0;
  34. matrix.Matrix42 = (float)0.0;
  35. matrix.Matrix43 = (float)0.0;
  36. matrix.Matrix44 = (float)0.0;
  37. matrix.Matrix33 = (float)1.0;
  38. matrix.Matrix44 = (float)1.0;
  39. //从0到1进行修改色彩变换矩阵主对角线上的数值
  40. //使三种基准色的饱和度渐增
  41. Single count = (float)0.0;
  42. while (count < 1.0)
  43. {
  44. matrix.Matrix00 = count;
  45. matrix.Matrix11 = count;
  46. matrix.Matrix22 = count;
  47. matrix.Matrix33 = count;
  48. attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  49. g.DrawImage(MyBitmap, new Rectangle(0, 0, width, height),
  50. 0, 0, width, height, GraphicsUnit.Pixel, attributes);
  51. private void button1_Click(object sender, EventArgs e)
  52. System.Threading.Thread.Sleep(200);
  53. count = (float)(count + 0.02);
  54. }
  55. }
  56. catch (Exception ex)
  57. {
  58. MessageBox.Show(ex.Message, "信息提示");
  59. }
  60. }

  61. private void button3_Click(object sender, EventArgs e)
  62. {

  63. try
  64. {
  65. Graphics g = this.panel1.CreateGraphics();
  66. g.Clear(Color.Gray);
  67. int width = MyBitmap.Width;
  68. int height = MyBitmap.Height;
  69. ImageAttributes attributes = new ImageAttributes();
  70. ColorMatrix matrix = new ColorMatrix();
  71. //创建淡出颜色矩阵
  72. matrix.Matrix00 = (float)0.0;
  73. matrix.Matrix01 = (float)0.0;
  74. matrix.Matrix02 = (float)0.0;
  75. matrix.Matrix03 = (float)0.0;
  76. matrix.Matrix04 = (float)0.0;
  77. matrix.Matrix10 = (float)0.0;
  78. matrix.Matrix11 = (float)0.0;
  79. matrix.Matrix12 = (float)0.0;
  80. matrix.Matrix13 = (float)0.0;
  81. matrix.Matrix14 = (float)0.0;
  82. matrix.Matrix20 = (float)0.0;
  83. matrix.Matrix21 = (float)0.0;
  84. matrix.Matrix22 = (float)0.0;
  85. matrix.Matrix23 = (float)0.0;
  86. matrix.Matrix24 = (float)0.0;
  87. matrix.Matrix30 = (float)0.0;
  88. matrix.Matrix31 = (float)0.0;
  89. matrix.Matrix32 = (float)0.0;
  90. matrix.Matrix33 = (float)0.0;
  91. matrix.Matrix34 = (float)0.0;
  92. matrix.Matrix40 = (float)0.0;
  93. matrix.Matrix41 = (float)0.0;
  94. matrix.Matrix42 = (float)0.0;
  95. matrix.Matrix43 = (float)0.0;
  96. matrix.Matrix44 = (float)0.0;
  97. matrix.Matrix33 = (float)1.0;
  98. matrix.Matrix44 = (float)1.0;
  99. //从1到0进行修改色彩变换矩阵主对角线上的数值
  100. //依次减少每种色彩分量
  101. Single count = (float)1.0;
  102. while (count > 0.0)
  103. {
  104. matrix.Matrix00 = (float)count;
  105. matrix.Matrix11 = (float)count;
  106. matrix.Matrix22 = (float)count;
  107. matrix.Matrix33 = (float)count;
  108. attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  109. g.DrawImage(MyBitmap, new Rectangle(0, 0, width, height),
  110. 0, 0, width, height, GraphicsUnit.Pixel, attributes);
  111. System.Threading.Thread.Sleep(20);
  112. count = (float)(count - 0.01);
  113. }
  114. }
  115. catch (Exception ex)
  116. {
  117. MessageBox.Show(ex.Message, "信息提示");
  118. }
  119. }



  120. 六、以左右对接的方式显示图像

  121. 原理:首先将图像分为左右两部分, 然后分别显示。

复制代码
六、以左右对接的方式显示图像
原理:首先将图像分为左右两部分, 然后分别显示。
代码:
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //以左右对接方式显示图像
  4. try
  5. {
  6. int width = this.MyBitmap.Width; //图像宽度
  7. int height = this.MyBitmap.Height; //图像高度
  8. Graphics g = this.panel1.CreateGraphics();
  9. g.Clear(Color.Gray); //初始为全灰色
  10. Bitmap bitmap = new Bitmap(width, height);
  11. int x = 0;
  12. while (x <= width / 2)
  13. {
  14. for (int i = 0; i <= height - 1; i++)
  15. {
  16. bitmap.SetPixel(x, i, MyBitmap.GetPixel(x, i));
  17. }
  18. for (int i = 0; i <= height - 1; i++)
  19. {
  20. bitmap.SetPixel(width - x - 1, i,
  21. MyBitmap.GetPixel(width - x - 1, i));
  22. }
  23. x++;
  24. this.panel1.Refresh();
  25. g.DrawImage (bitmap,0,0);
  26. System.Threading.Thread.Sleep(10);
  27. }
  28. }
  29. catch (Exception ex)
  30. {
  31. MessageBox.Show(ex.Message, "信息提示");
  32. }
  33. }
复制代码

七、以左右反转的方式显示图像
原理:计算图像位置和高度后以宽度的一半为轴进行对换左右半边的图像。
代码:
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //以左右反转方式显示图像
  4. try
  5. {
  6. int width = this.MyBitmap.Width; //图像宽度
  7. int height = this.MyBitmap.Height; //图像高度
  8. Graphics g = this.panel1.CreateGraphics();
  9. g.Clear(Color.Gray); //初始为全灰色
  10. for (int j = -height / 2; j <= height / 2; j++)
  11. {
  12. g.Clear(Color.Gray); //初始为全灰色
  13. int i = Convert.ToInt32(j * (Convert.ToSingle(width) / Convert.ToSingle(height)));
  14. Rectangle DestRect = new Rectangle(width / 2 - i, 0, 2 * i, height);
  15. Rectangle SrcRect = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);
  16. g.DrawImage(MyBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
  17. System.Threading.Thread.Sleep(10);
  18. }
  19. }
  20. catch (Exception ex)
  21. {
  22. MessageBox.Show(ex.Message, "信息提示");
  23. }
  24. }
复制代码

八、以从上向下拉伸的方式显示图像
原理:将图像的宽度不变每次显示图像的一部分, 直到将图片完全显示。
代码:
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //以从上向下拉伸方式显示图像
  4. try
  5. {
  6. int width = this.MyBitmap.Width; //图像宽度
  7. int height = this.MyBitmap.Height; //图像高度
  8. Graphics g = this.panel1.CreateGraphics();
  9. g.Clear(Color.Gray); //初始为全灰色
  10. for (int y = 1; y <= height; y++)
  11. {
  12. Bitmap bitmap=MyBitmap.Clone (new Rectangle(0,0,width ,y),
  13. System.Drawing .Imaging.PixelFormat .Format24bppRgb );
  14. g.DrawImage (bitmap,0,0);
  15. System.Threading.Thread.Sleep(10);
  16. }
  17. }
  18. catch (Exception ex)
  19. {
  20. MessageBox.Show(ex.Message, "信息提示");
  21. }
  22. }
复制代码

九、以从左向右拉伸的方式显示图像
原理:将图像的高度不变每次显示图像的一部分, 直到将图片完全显示。
代码:
  1. private void button1_Click(object sender, EventArgs e)
  2. {//以从左向右拉伸方式显示图像try
  3. {
  4. int width = this.MyBitmap.Width; //图像宽度
  5. int height = this.MyBitmap.Height; //图像高度
  6. Graphics g = this.panel1.CreateGraphics();g.Clear(Color.Gray); //初始为全灰色
  7. for (int x = 1; x <= width; x++)
  8. {
  9. Bitmap bitmap=MyBitmap.Clone (new Rectangle
  10. (0,0,x ,height),
  11. System.Drawing .Imaging.PixelFormat .Format24bppRgb );
  12. g.DrawImage (bitmap,0,0);
  13. System.Threading.Thread.Sleep(10);
  14. }
  15. }
  16. catch (Exception ex){MessageBox.Show(ex.Message, "信息提示");
  17. }
  18. }
复制代码

十、以任意角度旋转图像
原理:主要使用了 Graphics 类提供的 RotateTransform() 方法对图像进行旋转。
代码
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //以任意角度旋转显示图像
  4. Graphics g = this.panel1.CreateGraphics();
  5. float MyAngle = 0;//旋转的角度
  6. while (MyAngle < 360)
  7. {
  8. TextureBrush MyBrush = new TextureBrush(MyBitmap);
  9. this.panel1.Refresh();
  10. MyBrush.RotateTransform(MyAngle);
  11. g.FillRectangle(MyBrush, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height);
  12. MyAngle += 0.5f;
  13. System.Threading.Thread.Sleep(50);
  14. }
  15. }
复制代码

十一、以椭圆的方式显示图像
原理:主要使用了 Graphics 类提供的 FillEllipse() 方法和 TextureBrush() 方法。
代码:
十二、以不同的透明度显示图像.
原理:Graphics 类的 FromArgb() 方法。
代码:
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //以不同的透明度显示图像
  4. Graphics g = this.panel1.CreateGraphics();
  5. g.SmoothingMode = SmoothingMode.AntiAlias;
  6. TextureBrush MyBrush = new TextureBrush(MyBitmap);
  7. g.FillRectangle(MyBrush, this.panel1.ClientRectangle);
  8. for (int i = 0; i < 255; i++)
  9. {//由透明变为不透明
  10. g.FillRectangle(new SolidBrush(Color.FromArgb(i,Color.DarkSlateGray)), this.panel1.ClientRectangle);
  11. System.Threading.Thread.Sleep(100);
  12. }
  13. }
复制代码

十三、以不同分辨率显示图像
原理:Bitmap 类的 SetResolution 方法。
代码:
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //以不同的分辨率显示图像
  4. Graphics g = this.panel1.CreateGraphics();
  5. for (int i = 10; i < this.panel1.Height; i += 2)
  6. {
  7. g.Clear(Color.Gray);
  8. MyBitmap.SetResolution(i, i);
  9. g.DrawImage(MyBitmap, 0, 0);
  10. System.Threading.Thread.Sleep(100);
  11. }
  12. }
复制代码

十四、以不同翻转方式显示图像
原理:Bitmap 类的
RotateFip()方法。
代码:
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //以不同翻转方式显示图像
  4. Graphics g = this.panel1.CreateGraphics();
  5. for (int i = 0; i < 17; i++)
  6. {
  7. switch (i)
  8. {
  9. case 0:
  10. MyBitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);
  11. break;
  12. case 1:
  13. MyBitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
  14. break;
  15. case 2:
  16. MyBitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
  17. break;
  18. case 3:
  19. MyBitmap.RotateFlip(RotateFlipType.Rotate180FlipXY);
  20. break;
  21. case 4:
  22. MyBitmap.RotateFlip(RotateFlipType.Rotate180FlipY);
  23. break;
  24. case 5:
  25. MyBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  26. break;
  27. case 6:
  28. MyBitmap.RotateFlip(RotateFlipType.Rotate270FlipX);
  29. break;
  30. case 7:
  31. MyBitmap.RotateFlip(RotateFlipType.Rotate270FlipXY);
  32. break;
  33. case 8:
  34. MyBitmap.RotateFlip(RotateFlipType.Rotate270FlipY);
  35. break;
  36. case 9:
  37. MyBitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
  38. break;
  39. case 10:
  40. MyBitmap.RotateFlip(RotateFlipType.Rotate90FlipX);
  41. break;
  42. case 11:
  43. MyBitmap.RotateFlip(RotateFlipType.Rotate90FlipXY);
  44. break;
  45. case 12:
  46. MyBitmap.RotateFlip(RotateFlipType.Rotate90FlipY);
  47. break;
  48. case 13:
  49. MyBitmap.RotateFlip(RotateFlipType.RotateNoneFlipNone);
  50. break;
  51. case 14:
  52. MyBitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);
  53. break;
  54. case 15:
  55. MyBitmap.RotateFlip(RotateFlipType.RotateNoneFlipXY);
  56. break;
  57. case 16:
  58. MyBitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
  59. break;
  60. }
  61. g.Clear(Color.White);
  62. g.DrawImage(MyBitmap, 0, 0);
  63. System.Threading.Thread.Sleep(1000);
  64. }
  65. }
复制代码
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //椭圆显示图像
  4. this.panel1.Refresh();
  5. Graphics g = this.panel1.CreateGraphics();
  6. TextureBrush MyBrush = new TextureBrush(MyBitmap);
  7. g.FillEllipse(MyBrush, this.panel1.ClientRectangle);
  8. }
复制代码




上一篇:C#中通过读取配置文件动态创建泛型对象
下一篇:在asp.net中KindEditor编辑器的使用总结
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 17:23

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表