天天看點

以動畫的方式顯示圖像 (C# 制作)

 十一将至, 放假前将GDI+最後一部分今天終于完成: 以動畫的方式顯示圖像。希望對 GDI+程式設計的園友有所幫助.

PPT 以動畫方式顯示幻燈片是其一個很重要的特點,相信裡邊一定有您喜歡的動畫方式,今天我就帶大家認識幾款以動畫方式顯示幻燈片的制作方法,由于是GDI+程式設計, 這裡以圖像代替幻燈片(其實原理是相通的)來示範如何制作以動畫方式顯示圖像。

說明: 由于是以動畫方式顯示圖像, 這裡沒辦法直接貼靜态截圖, 是以決定給園友開源, 将所有的可運作代碼附在案例後面, 由于所有的動畫處理圖像的對象放在都pictureBox控件中, 同時定義的類都大同小異, 是以這裡先把下面案例中要用到的所有類及裝載圖像的代碼給大家, 運作時用這裡的代碼加下面任意一個執行個體的代碼即可運作程式! 同時樓主保證每個案例代碼都編譯通過, 絕不忽悠!

以動畫的方式顯示圖像 (C# 制作)
以動畫的方式顯示圖像 (C# 制作)

繪圖類定義及打開圖像檔案代碼

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;

}

}

一. 以上下反轉的方式顯示圖像.

原理: 計算圖像位置和高度後以高度的一半為軸進行對換上下半邊的圖像.

代碼: 

以動畫的方式顯示圖像 (C# 制作)
以動畫的方式顯示圖像 (C# 制作)

以上下反轉方式顯示圖像

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, "資訊提示");

}

}

二. 以上下對接的方式顯示圖像

原理: 首先将圖像分為上下兩部分, 然後分别顯示.

代碼: 

以動畫的方式顯示圖像 (C# 制作)
以動畫的方式顯示圖像 (C# 制作)

以上下對接方式顯示圖像

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, "資訊提示");

}

}

三. 以四周擴散的方式顯示圖像

原理: 首先設定圖像顯示的位置, 然後按高度和寬度的比例循環輸出, 直到高度和寬度為原始大小.

代碼:

以動畫的方式顯示圖像 (C# 制作)
以動畫的方式顯示圖像 (C# 制作)

以四周擴散方式顯示圖像

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方法從原圖指定的塊中複制圖像, 最後将這些塊依次顯示出來便可

代碼:

以動畫的方式顯示圖像 (C# 制作)
以動畫的方式顯示圖像 (C# 制作)

以分塊效果顯示圖像

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() 方法設定顔色, 調整矩陣實作淡出的效果. 此類還可以對顔色進行校正, 調暗, 調亮和移除等.

代碼: 

以動畫的方式顯示圖像 (C# 制作)
以動畫的方式顯示圖像 (C# 制作)

淡入顯示效果

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, "資訊提示");

}

}

以動畫的方式顯示圖像 (C# 制作)
以動畫的方式顯示圖像 (C# 制作)

淡出顯示圖像

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, "資訊提示");

}

}

六. 以左右對接的方式顯示圖像

原理: 首先将圖像分為左右兩部分, 然後分别顯示.

代碼:

以動畫的方式顯示圖像 (C# 制作)
以動畫的方式顯示圖像 (C# 制作)

以左右對接方式顯示圖像

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, "資訊提示");

}

}

七. 以左右反轉的方式顯示圖像

原理: 計算圖像位置和高度後以寬度的一半為軸進行對換左右半邊的圖像.\

代碼:

以動畫的方式顯示圖像 (C# 制作)
以動畫的方式顯示圖像 (C# 制作)

以左右反轉方式顯示圖像

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);

}

}

catch (Exception ex)

{

MessageBox.Show(ex.Message, "資訊提示");

}

}

八. 以從上向下拉伸的方式顯示圖像

原理: 将圖像的寬度不變每次顯示圖像的一部分, 直到将圖檔完全顯示.

代碼:

以動畫的方式顯示圖像 (C# 制作)
以動畫的方式顯示圖像 (C# 制作)

以從上向下拉伸方式顯示圖像

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 y = 1; y <= height; y++)

{

Bitmap bitmap=MyBitmap.Clone (new Rectangle(0,0,width ,y),

System.Drawing .Imaging.PixelFormat .Format24bppRgb );

g.DrawImage (bitmap,0,0);

System.Threading.Thread.Sleep(10);

}

}

catch (Exception ex)

{

MessageBox.Show(ex.Message, "資訊提示");

}

}

九. 以從左向右拉伸的方式顯示圖像

原理:  将圖像的高度不變每次顯示圖像的一部分, 直到将圖檔完全顯示

代碼: 

以動畫的方式顯示圖像 (C# 制作)
以動畫的方式顯示圖像 (C# 制作)

以從左向右拉伸方式顯示圖像

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 x = 1; x <= width; x++)

{

Bitmap bitmap=MyBitmap.Clone (new Rectangle

(0,0,x ,height),

System.Drawing .Imaging.PixelFormat .Format24bppRgb );

g.DrawImage (bitmap,0,0);

System.Threading.Thread.Sleep(10);

}

}

catch (Exception ex){MessageBox.Show(ex.Message, "資訊提示");

}

}

十. 以任意角度旋轉圖像

原理: 主要使用了 Graphics 類提供的 RotateTransform() 方法對圖像進行旋轉.

代碼: 

以動畫的方式顯示圖像 (C# 制作)
以動畫的方式顯示圖像 (C# 制作)

以任意角度旋轉顯示圖像

private void button1_Click(object sender, EventArgs e)

{

//以任意角度旋轉顯示圖像

Graphics g = this.panel1.CreateGraphics();

float MyAngle = 0;//旋轉的角度

while (MyAngle < 360)

{

TextureBrush MyBrush = new TextureBrush(MyBitmap);

this.panel1.Refresh();

MyBrush.RotateTransform(MyAngle);

g.FillRectangle(MyBrush, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height);

MyAngle += 0.5f;

System.Threading.Thread.Sleep(50);

}

}

十一. 以橢圓的方式顯示圖像

原理: 主要使用了 Graphics 類提供的 FillEllipse() 方法和 TextureBrush() 方法. 

代碼: 

以動畫的方式顯示圖像 (C# 制作)
以動畫的方式顯示圖像 (C# 制作)

橢圓顯示圖像

private void button1_Click(object sender, EventArgs e)

{

//橢圓顯示圖像

this.panel1.Refresh();

Graphics g = this.panel1.CreateGraphics();

TextureBrush MyBrush = new TextureBrush(MyBitmap);

g.FillEllipse(MyBrush, this.panel1.ClientRectangle);

}

十二. 以不同的透明度顯示圖像.

原理: Graphics 類的 FromArgb() 方法

代碼: 

以動畫的方式顯示圖像 (C# 制作)
以動畫的方式顯示圖像 (C# 制作)

以不同的透明度顯示圖像

private void button1_Click(object sender, EventArgs e)

{

//以不同的透明度顯示圖像

Graphics g = this.panel1.CreateGraphics();

g.SmoothingMode = SmoothingMode.AntiAlias;

TextureBrush MyBrush = new TextureBrush(MyBitmap);

g.FillRectangle(MyBrush, this.panel1.ClientRectangle);

for (int i = 0; i < 255; i++)

{//由透明變為不透明

g.FillRectangle(new SolidBrush(Color.FromArgb(i,Color.DarkSlateGray)), this.panel1.ClientRectangle);

System.Threading.Thread.Sleep(100);

}

}

十三. 以不同分辨率顯示圖像

原理: Bitmap 類的 SetResolution 方法

代碼: 

以動畫的方式顯示圖像 (C# 制作)
以動畫的方式顯示圖像 (C# 制作)

以不同的分辨率顯示圖像

private void button1_Click(object sender, EventArgs e)

{

//以不同的分辨率顯示圖像

Graphics g = this.panel1.CreateGraphics();

for (int i = 10; i < this.panel1.Height; i += 2)

{

g.Clear(Color.Gray);

MyBitmap.SetResolution(i, i);

g.DrawImage(MyBitmap, 0, 0);

System.Threading.Thread.Sleep(100);

}

}

十四. 以不同翻轉方式顯示圖像.

原理: Bitmap 類的 RotateFip()方法

代碼:

以動畫的方式顯示圖像 (C# 制作)
以動畫的方式顯示圖像 (C# 制作)

以不同翻轉方式顯示圖像

private void button1_Click(object sender, EventArgs e)

{

//以不同翻轉方式顯示圖像

Graphics g = this.panel1.CreateGraphics();

for (int i = 0; i < 17; i++)

{

switch (i)

{

case 0:

MyBitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);

break;

case 1:

MyBitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);

break;

case 2:

MyBitmap.RotateFlip(RotateFlipType.Rotate180FlipX);

break;

case 3:

MyBitmap.RotateFlip(RotateFlipType.Rotate180FlipXY);

break;

case 4:

MyBitmap.RotateFlip(RotateFlipType.Rotate180FlipY);

break;

case 5:

MyBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);

break;

case 6:

MyBitmap.RotateFlip(RotateFlipType.Rotate270FlipX);

break;

case 7:

MyBitmap.RotateFlip(RotateFlipType.Rotate270FlipXY);

break;

case 8:

MyBitmap.RotateFlip(RotateFlipType.Rotate270FlipY);

break;

case 9:

MyBitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);

break;

case 10:

MyBitmap.RotateFlip(RotateFlipType.Rotate90FlipX);

break;

case 11:

MyBitmap.RotateFlip(RotateFlipType.Rotate90FlipXY);

break;

case 12:

MyBitmap.RotateFlip(RotateFlipType.Rotate90FlipY);

break;

case 13:

MyBitmap.RotateFlip(RotateFlipType.RotateNoneFlipNone);

break;

case 14:

MyBitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);

break;

case 15:

MyBitmap.RotateFlip(RotateFlipType.RotateNoneFlipXY);

break;

case 16:

MyBitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);

break;

}

g.Clear(Color.White);

g.DrawImage(MyBitmap, 0, 0);

System.Threading.Thread.Sleep(1000);

}

}

十五. ...............

太多了, 大多都是一些GDI+類的常用方法, 如果感興趣的可以把幾個常用類熟悉一下.

自己也能實作很多個性化的以動畫方式顯示圖像. 

這裡連結前幾天寫的幾篇GDI+程式設計方面的文章, 需要補課的園友可以進去看一看.

第一課: C#制作 藝術字

第二課: C#制作 超酷的圖像效果

第三課: C#制作 各種統計圖 

c#