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

Posted

篇首语:知之者不如好之者,好之者不如乐之者。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 用C#来实现以动画的方式显示图像相关的知识,希望对你有一定的参考价值。

用C#来实现以动画的方式显示图像  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  说明

  由于是以动画方式显示图像 这里没办法直接贴静态截图 因此决定给园友开源 将所有的可运行代码附在案例后面 由于所有的动画处理图像的对象放在都pictureBox控件中 同时定义的类都大同小异 因此这里先把下面案例中要用到的所有类及装载图像的代码给大家 运行时用这里的代码加下面任意一个实例的代码即可运行程序!

  private Bitmap SourceBitmap; private Bitmap MyBitmap; private void button _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 pictureBox Width this pictureBox Height); this pictureBox Image = MyBitmap;

  一 以上下反转的方式显示图像

  原理 计算图像位置和高度后以高度的一半为轴进行对换上下半边的图像

  代码

  private void button _Click(object sender EventArgs e) try int width = this MyBitmap Width; //图像宽度 int height = this MyBitmap Height; //图像高度 Graphics g = this panel CreateGraphics(); g Clear(Color Gray); for (int i = width / ; i <= width / ; i++) g Clear(Color Gray); int j = Convert ToInt (i * (Convert ToSingle(height) / Convert ToSingle(width))); Rectangle DestRect = new Rectangle( height / j width * j); Rectangle SrcRect = new Rectangle( MyBitmap Width MyBitmap Height); g DrawImage(MyBitmap DestRect SrcRect GraphicsUnit Pixel); System Threading Thread Sleep( ); catch (Exception ex) MessageBox Show(ex Message 信息提示 );

  二 以上下对接的方式显示图像

  原理 首先将图像分为上下两部分 然后分别显示

  代码

  private void button _Click(object sender EventArgs e) try int width = this pictureBox Width; //图像宽度 int height = this pictureBox Height; //图像高度 Graphics g = this panel CreateGraphics(); g Clear(Color Gray); Bitmap bitmap = new Bitmap(width height); int x = ; while (x <= height / ) for (int i = ; i <= width ; i++) bitmap SetPixel(i x MyBitmap GetPixel(i x)); for (int i = ; i <= width ; i++) bitmap SetPixel(i height x MyBitmap GetPixel(i height x )); x++; this panel Refresh(); g DrawImage (bitmap ); System Threading Thread Sleep( ); catch (Exception ex) MessageBox Show(ex Message 信息提示 );

  三 以四周扩散的方式显示图像

  原理 首先设置图像显示的位置 然后按高度和宽度的比例循环输出 直到高度和宽度为原始大小

  代码

  

  private void button _Click(object sender EventArgs e) try int width = this MyBitmap Width; //图像宽度 int height = this MyBitmap Height; //图像高度 //取得Graphics对象 Graphics g = this panel CreateGraphics(); g Clear(Color Gray); //初始为全灰色 for (int i = ; i <= width / ; i++) int j = Convert ToInt (i*(Convert ToSingle(height) / Convert ToSingle(width))); Rectangle DestRect = new Rectangle(width / i height/ j * i *j); Rectangle SrcRect = new Rectangle( MyBitmap Width MyBitmap Height); g DrawImage(MyBitmap DestRect SrcRect GraphicsUnit Pixel); System Threading Thread Sleep( ); catch (Exception ex) MessageBox Show(ex Message 信息提示 );

  四 以分块效果显示图像

  原理 首先将图分为几块 再使用 Bitmap 类的 Clone方法从原图指定的块中复制图像

  最后将这些块依次显示出来便可

  代码

  

  private void button _Click(object sender EventArgs e) Graphics g = this panel CreateGraphics(); g Clear(Color White); int width = MyBitmap Width; int height = MyBitmap Height; //定义将图片切分成四个部分的区域 RectangleF[] block = new RectangleF( width/ height/ )

  new RectangleF(width/ width/ height/ ) new RectangleF( height/ width/ height/ ) new RectangleF(width/ height/ width/ height/ ); //分别克隆图片的四个部分 Bitmap[] MyBitmapBlack = MyBitmap Clone(block[ ] System Drawing Imaging PixelFormat DontCare) MyBitmap Clone(block[ ] System Drawing Imaging PixelFormat DontCare) MyBitmap Clone(block[ ] System Drawing Imaging PixelFormat DontCare) MyBitmap Clone(block[ ] System Drawing Imaging PixelFormat DontCare); //绘制图片的四个部分 各部分绘制时间间隔为 秒 g DrawImage(MyBitmapBlack[ ] ); System Threading Thread Sleep( ); g DrawImage(MyBitmapBlack[ ] width / ); System Threading Thread Sleep( ); g DrawImage(MyBitmapBlack[ ] width / height / ); System Threading Thread Sleep( ); g DrawImage(MyBitmapBlack[ ] height / );

  五 以淡入淡出效果显示图像

  原理 使用 ImageAttrributes 类的 SetColorMatrix() 方法设置颜色 调整矩阵实现淡出的效果 此类还可以对颜色进行校正 调暗 调亮和移除等

  代码

  

  private void button _Click(object sender EventArgs e) try Graphics g = this panel CreateGraphics(); g Clear(Color Gray); int width = MyBitmap Width; int height = MyBitmap Height; ImageAttributes attributes = new ImageAttributes(); ColorMatrix matrix = new ColorMatrix(); //创建淡入颜色矩阵 matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; //从 到 进行修改色彩变换矩阵主对角线上的数值 //使三种基准色的饱和度渐增 Single count = (float) ; while (count < ) matrix Matrix = count; matrix Matrix = count; matrix Matrix = count; matrix Matrix = count; attributes SetColorMatrix(matrix ColorMatrixFlag Default ColorAdjustType Bitmap); g DrawImage(MyBitmap new Rectangle( width height) width height GraphicsUnit Pixel attributes); private void button _Click(object sender EventArgs e) System Threading Thread Sleep( ); count = (float)(count + ); catch (Exception ex) MessageBox Show(ex Message 信息提示 ); private void button _Click(object sender EventArgs e) try Graphics g = this panel CreateGraphics(); g Clear(Color Gray); int width = MyBitmap Width; int height = MyBitmap Height; ImageAttributes attributes = new ImageAttributes(); ColorMatrix matrix = new ColorMatrix(); //创建淡出颜色矩阵 matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; matrix Matrix = (float) ; //从 到 进行修改色彩变换矩阵主对角线上的数值 //依次减少每种色彩分量 Single count = (float) ; while (count > ) matrix Matrix = (float)count; matrix Matrix = (float)count; matrix Matrix = (float)count; matrix Matrix = (float)count; attributes SetColorMatrix(matrix ColorMatrixFlag Default ColorAdjustType Bitmap); g DrawImage(MyBitmap new Rectangle( width height) width height GraphicsUnit Pixel attributes); System Threading Thread Sleep( ); count = (float)(count ); catch (Exception ex) MessageBox Show(ex Message 信息提示 );

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

  原理 首先将图像分为左右两部分 然后分别显示

  代码

  

  private void button _Click(object sender EventArgs e) //以左右对接方式显示图像 try int width = this MyBitmap Width; //图像宽度 int height = this MyBitmap Height; //图像高度 Graphics g = this panel CreateGraphics(); g Clear(Color Gray); //初始为全灰色 Bitmap bitmap = new Bitmap(width height); int x = ; while (x <= width / ) for (int i = ; i <= height ; i++) bitmap SetPixel(x i MyBitmap GetPixel(x i)); for (int i = ; i <= height ; i++) bitmap SetPixel(width x i MyBitmap GetPixel(width x i)); x++; this panel Refresh(); g DrawImage (bitmap ); System Threading Thread Sleep( ); catch (Exception ex) MessageBox Show(ex Message 信息提示 );

  七 以左右反转的方式显示图像

  原理 计算图像位置和高度后以宽度的一半为轴进行对换左右半边的图像

  代码

  

  private void button _Click(object sender EventArgs e) //以左右反转方式显示图像 try int width = this MyBitmap Width; //图像宽度 int height = this MyBitmap Height; //图像高度 Graphics g = this panel CreateGraphics(); g Clear(Color Gray); //初始为全灰色 for (int j = height / ; j <= height / ; j++) g Clear(Color Gray); //初始为全灰色 int i = Convert ToInt (j * (Convert ToSingle(width) / Convert ToSingle(height))); Rectangle DestRect = new Rectangle(width / i * i height); Rectangle SrcRect = new Rectangle( MyBitmap Width MyBitmap Height); g DrawImage(MyBitmap DestRect SrcRect GraphicsUnit Pixel); System Threading Thread Sleep( ); catch (Exception ex) MessageBox Show(ex Message 信息提示 );

  八 以从上向下拉伸的方式显示图像

  原理 将图像的宽度不变每次显示图像的一部分 直到将图片完全显示

  代码

  

  private void button _Click(object sender EventArgs e) //以从上向下拉伸方式显示图像 try int width = this MyBitmap Width; //图像宽度 int height = this MyBitmap Height; //图像高度 Graphics g = this panel CreateGraphics(); g Clear(Color Gray); //初始为全灰色 for (int y = ; y <= height; y++) Bitmap bitmap=MyBitmap Clone (new Rectangle( width y) System Drawing Imaging PixelFormat Format bppRgb ); g DrawImage (bitmap ); System Threading Thread Sleep( ); catch (Exception ex) MessageBox Show(ex Message 信息提示 );

  九 以从左向右拉伸的方式显示图像

  原理 将图像的高度不变每次显示图像的一部分 直到将图片完全显示

  代码

  

  private void button _Click(object sender EventArgs e) //以从左向右拉伸方式显示图像try int width = this MyBitmap Width; //图像宽度 int height = this MyBitmap Height; //图像高度 Graphics g = this panel CreateGraphics();g Clear(Color Gray); //初始为全灰色 for (int x = ; x <= width; x++) Bitmap bitmap=MyBitmap Clone (new Rectangle ( x height) System Drawing Imaging PixelFormat Format bppRgb ); g DrawImage (bitmap ); System Threading Thread Sleep( ); catch (Exception ex)MessageBox Show(ex Message 信息提示 );

  十 以任意角度旋转图像

  原理 主要使用了 Graphics 类提供的 RotateTransform() 方法对图像进行旋转

  代码

  

  private void button _Click(object sender EventArgs e) //以任意角度旋转显示图像 Graphics g = this panel CreateGraphics(); float MyAngle = ;//旋转的角度 while (MyAngle < ) TextureBrush MyBrush = new TextureBrush(MyBitmap); this panel Refresh(); MyBrush RotateTransform(MyAngle); g FillRectangle(MyBrush this ClientRectangle Width this ClientRectangle Height); MyAngle += f; System Threading Thread Sleep( );

  十一 以椭圆的方式显示图像

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

  代码

  十二 以不同的透明度显示图像

  原理 Graphics 类的 FromArgb() 方法

  代码

  

  private void button _Click(object sender EventArgs e) //以不同的透明度显示图像 Graphics g = this panel CreateGraphics(); g SmoothingMode = SmoothingMode AntiAlias; TextureBrush MyBrush = new TextureBrush(MyBitmap); g FillRectangle(MyBrush this panel ClientRectangle); for (int i = ; i < ; i++) //由透明变为不透明 g FillRectangle(new SolidBrush(Color FromArgb(i Color DarkSlateGray)) this panel ClientRectangle); System Threading Thread Sleep( );

  十三 以不同分辨率显示图像

  原理 Bitmap 类的 SetResolution 方法

  代码

  

  private void button _Click(object sender EventArgs e) //以不同的分辨率显示图像 Graphics g = this panel CreateGraphics(); for (int i = ; i < this panel Height; i += ) g Clear(Color Gray); MyBitmap SetResolution(i i); g DrawImage(MyBitmap ); System Threading Thread Sleep( );

  十四 以不同翻转方式显示图像

  原理 Bitmap 类的

  RotateFip()方法

  代码

  

  private void button _Click(object sender EventArgs e) //以不同翻转方式显示图像 Graphics g = this panel CreateGraphics(); for (int i = ; i < ; i++) switch (i) case : MyBitmap RotateFlip(RotateFlipType RotateNoneFlipX); break; case : MyBitmap RotateFlip(RotateFlipType Rotate FlipNone); break; case : MyBitmap RotateFlip(RotateFlipType Rotate FlipX); break; case : MyBitmap RotateFlip(RotateFlipType Rotate FlipXY); break; case : MyBitmap RotateFlip(RotateFlipType Rotate FlipY); break; case : MyBitmap RotateFlip(RotateFlipType Rotate FlipNone); break; case : MyBitmap RotateFlip(RotateFlipType Rotate FlipX); break; case : MyBitmap RotateFlip(RotateFlipType Rotate FlipXY); break; case : MyBitmap RotateFlip(RotateFlipType Rotate FlipY); break; case : MyBitmap RotateFlip(RotateFlipType Rotate FlipNone); break; case : MyBitmap RotateFlip(RotateFlipType Rotate FlipX); break; case : MyBitmap RotateFlip(RotateFlipType Rotate FlipXY); break; case : MyBitmap RotateFlip(RotateFlipType Rotate FlipY); break; case : MyBitmap RotateFlip(RotateFlipType RotateNoneFlipNone); break; case : MyBitmap RotateFlip(RotateFlipType RotateNoneFlipX); break; case : MyBitmap RotateFlip(RotateFlipType RotateNoneFlipXY); break; case : MyBitmap RotateFlip(RotateFlipType RotateNoneFlipY); break; g Clear(Color White); g DrawImage(MyBitmap ); System Threading Thread Sleep( );

  

  private void button _Click(object sender EventArgs e) //椭圆显示图像 this panel Refresh(); Graphics g = this panel CreateGraphics(); TextureBrush MyBrush = new TextureBrush(MyBitmap); g FillEllipse(MyBrush this panel ClientRectangle);

cha138/Article/program/net/201311/11180

相关参考

知识大全 C# 制作以动画的方式显示图像[2]

C#制作以动画的方式显示图像[2]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &nb

知识大全 C# 制作以动画的方式显示图像[3]

C#制作以动画的方式显示图像[3]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &nb

知识大全 C# 制作以动画的方式显示图像[4]

C#制作以动画的方式显示图像[4]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! mat

知识大全 C# 制作以动画的方式显示图像[8]

C#制作以动画的方式显示图像[8]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &nb

知识大全 C# 制作以动画的方式显示图像[6]

C#制作以动画的方式显示图像[6]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! try

知识大全 C# 制作以动画的方式显示图像[9]

C#制作以动画的方式显示图像[9]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &nb

知识大全 C# 制作以动画的方式显示图像[10]

C#制作以动画的方式显示图像[10]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! br

知识大全 C# 制作以动画的方式显示图像[1]

C#制作以动画的方式显示图像[1]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  十一将至放假前

知识大全 C# 制作以动画的方式显示图像[7]

C#制作以动画的方式显示图像[7]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  八以从上向下拉

下列数字图像的文件格式中,能够在网页上发布并可以具有动画效果的是

下列数字图像的文件格式中,能够在网页上发布并可以具有动画效果的是_____。A、BMPB、GIFC、JPEGD、TIFF答案:B解析:GIF文件格式的数字图像具有动画效果。