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

本文介绍了一系列图像处理技巧,包括上下反转、对接显示、四周扩散等效果的实现方法,并提供了详细的C#代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因此这里先把下面案例中要用到的所有类及装载图像的代码给大家,运行时用这里的代码加下面任意一个实例的代码即可运行程序!
lYnq#4Uj^ [ 此文转贴于 贵 州 学 习 网 网络编程.Net教程 http://WWW.gzu521.COM ] lYnq#4Uj^
 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, "信息提示");
  }
  }

七、以左右反转的方式显示图像
原理:计算图像位置和高度后以宽度的一半为轴进行对换左右半边的图像。
代码:
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, "信息提示");
  }
  }

 

八、以从上向下拉伸的方式显示图像
原理:将图像的宽度不变每次显示图像的一部分, 直到将图片完全显示。
代码:
q} \Q.=n 来 源 于 贵 州学 习网 网络编程.Net教程 WWw.gzU521.COm q} \Q.=n
 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, "信息提示");
  }
  }

 

九、以从左向右拉伸的方式显示图像
原理:将图像的高度不变每次显示图像的一部分, 直到将图片完全显示。
代码:
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() 方法对图像进行旋转。
代码:
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() 方法。
代码:
 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() 方法。
代码:
  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 方法。
代码:
 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()方法。
代码:
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;

转载于:https://www.cnblogs.com/bihailantian/archive/2010/09/23/1833466.html

LayeredForm : 支持窗体动画特效,透明,可以和LayeredControl实现任意透明效果等。。。包含LayeredWindowForm的功能 支持一部分带有Paint事件的普通控件,但是不能实现普通控件的背景透明效果! 控件类:Controls HotKey:支持全局热键绑定,事件驱动,可以开启和关闭 LayeredButton:按钮控件,支持按钮颜色设置,图片按钮,如果只设置一张正常状态下的按钮图片,则有鼠标移入加亮效果和鼠标按下变暗效果。边框设置,文字效果设置。 LayeredCheckButton:对LayeredButton的扩展,支持状态切换。 LayeredDragBar:支持对父容器的尺寸拖拽调整 LayeredFlashBox:支持透明Flash播放(当前版本不可用,请勿使用!) LayeredLabel:对文字的显示,文字效果设置 LayeredListBox:支自定义列表项目,支持横向和纵向滚动,支持平滑滚动。 LayeredPanel:在Layered模式下的容器控件 LayeredPictureBox:支持Gif 播放,支持多张图片合成动画播放。播放Gif时候不要频繁暂停和播放动画,容易导致线程阻塞。 LayeredTextBox:Layered模式下的文本编辑器。支持水印文字设置 LayeredTrackBar:进度条控件,支持图片进度条定义 动画类:Animations 通过设置LayeredForm的Animation.Effect属性来定义窗体动画特效。 包含了以下特效类:BlindWindowEffect、FadeinFadeoutEffect、GradualCurtainEffect、LevelScrollEffect、RandomCurtainEffect、RotateZoomEffect、ThreeDTurn、ZoomEffect 可以通过实现IEffects接口来实现自定义特效 DirectUI类:DirectUI 包含几个DirectUI控件。用于对以上控件的扩展和美化。部分LayeredControl包含DUIControl属性可以向其添加DirectUI控件。支持通过集合编辑器里面添加,只是不能在集合编辑器里面绑定事件,需要手动写代码绑定。通过集合编辑器添加的控件不一定能马上在设计器里看到效果,因为会有图像缓存,可能需要尝试调整控件大小等方式强制控件重绘,就可以看到效果了。 LayeredControl可以支持在普通窗体上使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值