C# 製作以動畫的方式顯示圖像【轉】

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);
System.Threading.Thread.Sleep(200);
  count = (float)(count + 0.02);
  }
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message, "信息提示");
  }
  }
   
  private void button3_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;
  //從1到0進行修改色彩變換矩陣主對角線上的數值
  //依次減少每種色彩分量
  Single count = (float)1.0;
  while (count > 0.0)
  {
  matrix.Matrix00 = (float)count;
  matrix.Matrix11 = (float)count;
  matrix.Matrix22 = (float)count;
  matrix.Matrix33 = (float)count;
  attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  g.DrawImage(MyBitmap, new Rectangle(0, 0, width, height),
  0, 0, width, height, GraphicsUnit.Pixel, attributes);
  System.Threading.Thread.Sleep(20);
  count = (float)(count - 0.01);
  }
  }
  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 g = this.panel1.CreateGraphics();
  g.Clear(Color.Gray); //初始爲全灰色
  Bitmap bitmap = new Bitmap(width, height);
  int x = 0;
  while (x <= width / 2)
  {
  for (int i = 0; i <= height - 1; i++)
  {
  bitmap.SetPixel(x, i, MyBitmap.GetPixel(x, i));
  }
  for (int i = 0; i <= height - 1; i++)
  {
  bitmap.SetPixel(width - x - 1, i,
  MyBitmap.GetPixel(width - x - 1, i));
  }
  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 g = this.panel1.CreateGraphics();
  g.Clear(Color.Gray); //初始爲全灰色
  for (int j = -height / 2; j <= height / 2; j++)
  {
  g.Clear(Color.Gray); //初始爲全灰色
  int i = Convert.ToInt32(j * (Convert.ToSingle(width) / Convert.ToSingle(height)));
  Rectangle DestRect = new Rectangle(width / 2 - i, 0, 2 * i, height);
  Rectangle SrcRect = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);
  g.DrawImage(MyBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
  System.Threading.Thread.Sleep(10);

 

轉自:http://bbs.techrepublic.com.cn/thread-726690-1-1.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章