自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

實現(xiàn)C#顯示圖像的方式

開發(fā) 后端
本文介紹以上下反轉(zhuǎn)的方式實現(xiàn)C#顯示圖像,以上下對接的方式實現(xiàn)C#顯示圖像和以四周擴(kuò)散的方式顯示圖像。

說明:

由于是以動畫方式顯示圖像,這里沒辦法直接貼靜態(tài)截圖,因此決定給園友開源,將所有的可運行代碼附在案例后面,由于所有的動畫處理圖像的對象放在都 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. }  

一、以上下反轉(zhuǎn)的方式實現(xiàn)C#顯示圖像.

原理:計算圖像位置和高度后以高度的一半為軸進(jìn)行對換上下半邊的圖像。

代碼:

  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. }  

二、以上下對接的方式實現(xiàn)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. }  

三、以四周擴(kuò)散的方式顯示圖像

原理:首先設(shè)置圖像顯示的位置, 然后按高度和寬度的比例循環(huán)輸出, 直到高度和寬度為原始大小。

代碼:

  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關(guān)鍵字全解析
  2. 淺談C#中構(gòu)造函數(shù)和成員函數(shù)
  3. C#回調(diào)函數(shù)及API應(yīng)用淺析
  4. 詳解C# Object.Equals函數(shù)
  5. C#調(diào)用Windows API函數(shù)
責(zé)任編輯:佚名 來源: IT168
相關(guān)推薦

2009-09-03 09:44:02

DropDownLisC#遞歸

2009-08-25 09:19:01

C#實現(xiàn)窗體顯示

2009-08-25 11:10:20

C#編程實現(xiàn)顯示XML

2024-12-20 09:48:47

C#Python代碼

2024-10-15 08:29:09

C#軟件開發(fā)

2009-09-08 15:12:07

C# ListBox

2009-05-13 11:50:17

C#多繼承接口

2009-08-07 12:57:03

C#讀取Excel

2011-06-07 13:44:40

VC

2009-05-26 16:33:48

PythonC#Run As

2024-05-10 07:44:23

C#進(jìn)程程序

2024-08-13 08:25:16

C#外部程序方式

2024-05-27 00:20:00

2024-09-13 08:27:00

2009-08-27 14:01:41

C#進(jìn)度條

2009-08-26 09:54:45

C#打印預(yù)覽C#打印

2009-09-01 18:29:10

C#繼承C#多態(tài)

2024-04-01 11:30:57

C#拷貝

2009-06-09 15:52:40

C#指針圖像操

2009-08-25 10:59:00

C#調(diào)用函數(shù)顯示值
點贊
收藏

51CTO技術(shù)棧公眾號