注册本站  论坛  繁體中文

慧民电脑芯片级维修-电脑技巧
手机 | MP3 | MP4 | 显卡 | 主板 | 显示器 | 光存储 | 笔记本 | 网络设备 | 移动存储 | 数码相机
键鼠 | CPU | 音箱 | GPS | 电视 | 服务器 | 投影机 | 机箱电源 | 品牌电脑 | 办公打印 |
| 网站首页 | Cisco | Windows | Linux | Java | Dotnet | Oracle | 网页设计 | 平面设计 | 安全 | 软件应用 | 电脑维修 | 办公维修 |
您现在的位置: 电脑技巧 >> Dotnet >> C# >> Dotnet正文

C# 制作以动画的方式显示图像

文章来源:aspcool 作者:佚名 更新时间:2008-10-6 13:55:24 【 】 【加入收藏

  十一将至, 放假前将GDI+最后一部分今天终于完成: 以动画的方式显示图像。希望对 GDI+编程的园友有所帮助。
 
  PPT 以动画方式显示幻灯片是其一个很重要的特点,相信里边一定有您喜欢的动画方式,今天我就带大家认识几款以动画方式显示幻灯片的制作方法,由于是GDI+编程, 这里以图像代替幻灯片(其实原理是相通的)来演示如何制作以动画方式显示图像。
 
  说明: 由于是以动画方式显示图像, 这里没办法直接贴静态截图, 因此决定给园友开源, 将所有的可运行代码附在案例后面, 由于所有的动画处理图像的对象放在都pictureBox控件中, 同时定义的类都大同小异, 因此这里先把下面案例中要用到的所有类及装载图像的代码给大家, 运行时用这里的代码加下面任意一个实例的代码即可运行程序! 同时楼主保证每个案例代码都编译通过, 绝不忽悠!
 
   private Bitmap SourceBitmap;
  private Bitmap MyBitmap;
  private void button2_Click(object sender, EventArgs e)
  {
  //打开图像文件
  OpenFileDialog openFileDialog = new OpenFileDialog();
  openFileDialog.Filter = "图像文件(JPeg, Gif, Bmp, etc.)
  |*.jpg;*.jpeg;*.gif;*.bmp;*.tif; *.tiff; *.png| JPeg 图像文件(*.jpg;*.jpeg)
  |*.jpg;*.jpeg |GIF 图像文件(*.gif)|*.gif |BMP图像文件(*.bmp)|*.bmp
  |Tiff图像文件(*.tif;*.tiff)|*.tif;*.tiff|Png图像文件(*.png)| *.png |所有文件(*.*)|*.*";
  if (openFileDialog.ShowDialog() == DialogResult.OK)
  {
  //得到原始大小的图像
  SourceBitmap = new Bitmap(openFileDialog.FileName);
  //得到缩放后的图像
  MyBitmap = new Bitmap(SourceBitmap, this.pictureBox1.Width, this.pictureBox1.Height);
  this.pictureBox1.Image = MyBitmap;
  }
  } 
   
   一. 以上下反转的方式显示图像.
  
  原理: 计算图像位置和高度后以高度的一半为轴进行对换上下半边的图像.
  
  代码:
  
  
  private void button1_Click(object sender, EventArgs e)
  {
  
  try
  {
  int width = this.MyBitmap.Width; //图像宽度
  int height = this.MyBitmap.Height; //图像高度
  Graphics g = this.panel1.CreateGraphics();
  g.Clear(Color.Gray);
  for (int i = -width / 2; i <= width / 2; i++)
  {
  g.Clear(Color.Gray);
  int j = Convert.ToInt32(i * (Convert.ToSingle(height) / Convert.ToSingle(width)));
  Rectangle DestRect = new Rectangle(0, height / 2 -j, width, 2 * j);
  Rectangle SrcRect = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);
  g.DrawImage(MyBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
  System.Threading.Thread.Sleep(10);
  }
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message, "信息提示");
  }
  } 
   
   
  二. 以上下对接的方式显示图像
  
  原理: 首先将图像分为上下两部分, 然后分别显示.
  
  代码:
  
  
  private void button1_Click(object sender, EventArgs e)
  {
  
  try
  {
  int width = this.pictureBox1.Width; //图像宽度
  int height = this.pictureBox1.Height; //图像高度
  Graphics g = this.panel1.CreateGraphics();
  g.Clear(Color.Gray);
  Bitmap bitmap = new Bitmap(width, height);
  int x = 0;
  while (x <= height / 2)
  {
  for (int i = 0; i <= width - 1; i++)
  {
  bitmap.SetPixel(i, x, MyBitmap.GetPixel(i, x));
  }
  for (int i = 0; i <= width - 1; i++)
  {
  bitmap.SetPixel(i, height - x - 1, MyBitmap.GetPixel(i, height - x - 1));
  }
  x++;
  this.panel1.Refresh();
  g.DrawImage (bitmap,0,0);
  System.Threading.Thread.Sleep(10);
  }
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message, "信息提示");
  }
  }
  
  
  
  
  三. 以四周扩散的方式显示图像
  
  原理: 首先设置图像显示的位置, 然后按高度和宽度的比例循环输出, 直到高度和宽度为原始大小.
  
  代码:
  
  
  private void button1_Click(object sender, EventArgs e)
  {
  
  try
  {
  int width = this.MyBitmap.Width; //图像宽度
  int height = this.MyBitmap.Height; //图像高度
  //取得Graphics对象
  Graphics g = this.panel1.CreateGraphics();
  g.Clear(Color.Gray); //初始为全灰色
  for (int i = 0; i <= width / 2; i++)
  {
  int j = Convert.ToInt32 (i*(Convert.ToSingle(height) / Convert.ToSingle(width)));
  Rectangle DestRect = new Rectangle(width / 2 - i, height/2-j, 2 * i, 2*j);
  Rectangle SrcRect = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);
  g.DrawImage(MyBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
  System.Threading.Thread.Sleep(10);
  }
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message, "信息提示");
  }
  }
  
  
  
  
  四. 以分块效果显示图像
  
  原理: 首先将图分为几块, 再使用 Bitmap 类的 Clone方法从原图指定的块中复制图像, 最后将这些块依次显示出来便可
  
  代码:
  
  
  private void button1_Click(object sender, EventArgs e)
  {
  
  Graphics g = this.panel1.CreateGraphics();
  g.Clear(Color.White);
  int width = MyBitmap.Width;
  int height = MyBitmap.Height;
  //定义将图片切分成四个部分的区域
  RectangleF[] block ={
  new RectangleF(0,0,width/2,height/2),
  new RectangleF(width/2,0,width/2,height/2),
  new RectangleF(0,height/2,width/2,height/2),
  new RectangleF(width/2,height/2,width/2,height/2)};
  //分别克隆图片的四个部分
  Bitmap[] MyBitmapBlack ={
  MyBitmap.Clone(block[0],System.Drawing.Imaging.PixelFormat.DontCare),
  MyBitmap.Clone(block[1],System.Drawing.Imaging.PixelFormat.DontCare),
  MyBitmap.Clone(block[2],System.Drawing.Imaging.PixelFormat.DontCare),
  MyBitmap.Clone(block[3],System.Drawing.Imaging.PixelFormat.DontCare)};
  //绘制图片的四个部分,各部分绘制时间间隔为0.5秒
  g.DrawImage(MyBitmapBlack[0], 0, 0);
  System.Threading.Thread.Sleep(1000);
  g.DrawImage(MyBitmapBlack[1], width / 2, 0);
  System.Threading.Thread.Sleep(1000);
  g.DrawImage(MyBitmapBlack[3], width / 2, height / 2);
  System.Threading.Thread.Sleep(1000);
  g.DrawImage(MyBitmapBlack[2], 0, height / 2);
  }
  
  
  
  
  五. 以淡入淡出效果显示图像
  
  原理: 使用 ImageAttrributes 类的 SetColorMatrix() 方法设置颜色, 调整矩阵实现淡出的效果. 此类还可以对颜色进行校正, 调暗, 调亮和移除等.
  
  代码:
  
  
  private void button1_Click(object sender, EventArgs e)
  {
  
  try
  {
  Graphics g = this.panel1.CreateGraphics();
  g.Clear(Color.Gray);
  int width = MyBitmap.Width;
  int height = MyBitmap.Height;
  ImageAttributes attributes = new ImageAttributes();
  ColorMatrix matrix = new ColorMatrix();
  //创建淡入颜色矩阵
  matrix.Matrix00 = (float)0.0;
  matrix.Matrix01 = (float)0.0;
  matrix.Matrix02 = (float)0.0;
  matrix.Matrix03 = (float)0.0;
  matrix.Matrix04 = (float)0.0;
  matrix.Matrix10 = (float)0.0;
  matrix.Matrix11 = (float)0.0;
  matrix.Matrix12 = (float)0.0;
  matrix.Matrix13 = (float)0.0;
  matrix.Matrix14 = (float)0.0;
  matrix.Matrix20 = (float)0.0;
  matrix.Matrix21 = (float)0.0;
  matrix.Matrix22 = (float)0.0;
  matrix.Matrix23 = (float)0.0;
  matrix.Matrix24 = (float)0.0;
  matrix.Matrix30 = (float)0.0;
  matrix.Matrix31 = (float)0.0;
  matrix.Matrix32 = (float)0.0;
  matrix.Matrix33 = (float)0.0;
  matrix.Matrix34 = (float)0.0;
  matrix.Matrix40 = (float)0.0;
  matrix.Matrix41 = (float)0.0;
  matrix.Matrix42 = (float)0.0;
  matrix.Matrix43 = (float)0.0;
  matrix.Matrix44 = (float)0.0;
  matrix.Matrix33 = (float)1.0;
  matrix.Matrix44 = (float)1.0;
  //从0到1进行修改色彩变换矩阵主对角线上的数值
  //使三种基准色的饱和度渐增
  Single count = (float)0.0;
  while (count < 1.0)
  {
  matrix.Matrix00 = count;
  matrix.Matrix11 = count;
  matrix.Matrix22 = count;
  matrix.Matrix33 = count;
  attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  g.DrawImage(MyBitmap, new Rectangle(0, 0, width, height),
  0, 0, width, height, GraphicsUnit.Pixel, attributes);

  • 上一篇Dotnet:

  • 下一篇Dotnet:
  • 最 新 热 门
     用VB.net2008编写屏幕抓捕程序
     用VB.net2008编写幻灯片程序
     VB.net2008创建发送与接收端程序
     在VB.NET中使用动态属性解析
     ASP.NET中图象处理过程详解
     解析VB.NET中的面向对象编程特征
     VisualC#中实现DB2数据库编程
     在VisualC#中用ListView显示数据记录
     用Dojo和Ajax创建可重用和可重新发布
     ASP.NET网络编程中经常用到的27个函数集
    最 新 推 荐
     利用C#实现超酷动态图像按钮
     C#开发智能手机游戏推箱子软件
     C# 索引器实战开发学习笔记
     编程中使用C#的BitmapData实例
     C# 制作以动画的方式显示图像
     如何构造一个C#语言的爬虫蜘蛛程序
     C#编程轻松实现对文件的操作技巧
     C#如何取硬件标志代码
     C#中将Big5繁体转换简体GB2312的代码
     C#线程池的实现
    相 关 文 章

    VisualC#中实现DB2数据库编程
    在VisualC#中用ListView显示数据记录
    利用C#实现超酷动态图像按钮
    C#开发智能手机游戏推箱子软件
    C# 索引器实战开发学习笔记
    编程中使用C#的BitmapData实例
    C#和Visual Basic的匿名类型区别
    Visual C#中实现DB2数据库的编程实例
    Visual C#实现DB2数据库的编程例子
    Visual C#中实现DB2数据库的编程

    | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告

     

    Copyright 2006-2008 pcjx.com All Rights Reserved
    电脑技巧 版权所有 粤ICP备06059145号 地图
    门市地址:广东省佛山市南海区黄岐黄海路133号
    本网站所有内容未经许可不得转载或做其他使用