实现C 实现创新驱动发展最根本的是什么
创始人
2024-04-23 15:41:09
0

说明:

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

  1. privateBitmapSourceBitmap;  
  2. privateBitmapMyBitmap;  
  3. privatevoidbutton2_Click(objectsender,EventArgse)  
  4. {  
  5. //打开图像文件  
  6. OpenFileDialogopenFileDialog=newOpenFileDialog();  
  7. openFileDialog.Filter="图像文件(JPeg,Gif,Bmp,etc.)  
  8. |*.jpg;*.jpeg;*.gif;*.bmp;*.tif;*.tiff;*.png|  
  9. JPeg图像文件(*.jpg;*.jpeg)  
  10. |*.jpg;*.jpeg|GIF图像文件(*.gif)|*.gif|BMP图像文件(*.bmp)|*.bmp  
  11. |Tiff图像文件(*.tif;*.tiff)|*.tif;*.tiff|Png图像  
  12. 文件(*.png)|*.png|所有文件(*.*)|*.*";  
  13. if(openFileDialog.ShowDialog()==DialogResult.OK)  
  14. {  
  15. //得到原始大小的图像  
  16. SourceBitmap=newBitmap(openFileDialog.FileName);  
  17. //得到缩放后的图像  
  18. MyBitmap=newBitmap(SourceBitmap,this.pictureBox1.Width,this  
  19. .pictureBox1.Height);  
  20. this.pictureBox1.Image=MyBitmap;  
  21. }  
  22. }  

一、以上下反转的方式实现C#显示图像.

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

代码:

  1. privatevoidbutton1_Click(objectsender,EventArgse)  
  2. {  
  3.  
  4. try  
  5. {  
  6. intwidth=this.MyBitmap.Width;//图像宽度  
  7. intheight=this.MyBitmap.Height;//图像高度  
  8. Graphicsg=this.panel1.CreateGraphics();  
  9. g.Clear(Color.Gray);  
  10. for(inti=-width/2;i<=width/2;i++)  
  11. {  
  12. g.Clear(Color.Gray);  
  13. intj=Convert.ToInt32(i*(Convert.ToSingle(height)/Convert.ToS  
  14. ingle(width)));  
  15. RectangleDestRect=newRectangle(0,height/2-j,width,2*j);  
  16. RectangleSrcRect=newRectangle(0,0,MyBitmap.Width,MyBitmap.Height);  
  17. g.DrawImage(MyBitmap,DestRect,SrcRect,GraphicsUnit.Pixel);  
  18. System.Threading.Thread.Sleep(10);  
  19. }  
  20. }  
  21. catch(Exceptionex)  
  22. {  
  23. MessageBox.Show(ex.Message,"信息提示");  
  24. }  
  25. }  

二、以上下对接的方式实现C#显示图像

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

代码:

  1. privatevoidbutton1_Click(objectsender,EventArgse)  
  2. {  
  3.  
  4. try  
  5. {  
  6. intwidth=this.pictureBox1.Width;//图像宽度  
  7. intheight=this.pictureBox1.Height;//图像高度  
  8. Graphicsg=this.panel1.CreateGraphics();  
  9. g.Clear(Color.Gray);  
  10. Bitmapbitmap=newBitmap(width,height);  
  11. intx=0;  
  12. while(x<=height/2)  
  13. {  
  14. for(inti=0;i<=width-1;i++)  
  15. {  
  16. bitmap.SetPixel(i,x,MyBitmap.GetPixel(i,x));  
  17. }  
  18. for(inti=0;i<=width-1;i++)  
  19. {  
  20. bitmap.SetPixel(i,height-x-1,MyBitmap.GetPixel(i,height-x-1));  
  21. }  
  22. x++;  
  23. this.panel1.Refresh();  
  24. g.DrawImage(bitmap,0,0);  
  25. System.Threading.Thread.Sleep(10);  
  26. }  
  27. }  
  28. catch(Exceptionex)  
  29. {  
  30. MessageBox.Show(ex.Message,"信息提示");  
  31. }  
  32. }  

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

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

代码:

  1. privatevoidbutton1_Click(objectsender,EventArgse)  
  2. {  
  3. try  
  4. {  
  5. intwidth=this.MyBitmap.Width;//图像宽度  
  6. intheight=this.MyBitmap.Height;//图像高度  
  7. //取得Graphics对象  
  8. Graphicsg=this.panel1.CreateGraphics();  
  9. g.Clear(Color.Gray);//初始为全灰色  
  10. for(inti=0;i<=width/2;i++)  
  11. {  
  12. intj=Convert.ToInt32(i*(Convert.ToSingle(height)/Convert.ToSingle(width)));  
  13. RectangleDestRect=newRectangle(width/2-i,height/2-j,2*i,2*j);  
  14. RectangleSrcRect=newRectangle(0,0,MyBitmap.Width,MyBitmap.Height);  
  15. g.DrawImage(MyBitmap,DestRect,SrcRect,GraphicsUnit.Pixel);  
  16. System.Threading.Thread.Sleep(10);  
  17. }  
  18. }  
  19. catch(Exceptionex)  
  20. {  
  21. MessageBox.Show(ex.Message,"信息提示");  
  22. }  

【编辑推荐】

  1. C# 4.0 Dynamic关键字全解析
  2. 浅谈C#中构造函数和成员函数
  3. C#回调函数及API应用浅析
  4. 详解C# Object.Equals函数
  5. C#调用Windows API函数

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...