實現(xiàn)C#顯示圖像的方式
說明:
由于是以動畫方式顯示圖像,這里沒辦法直接貼靜態(tài)截圖,因此決定給園友開源,將所有的可運行代碼附在案例后面,由于所有的動畫處理圖像的對象放在都 pictureBox控件中,同時定義的類都大同小異,因此這里先把下面案例中要用到的所有類及裝載圖像的代碼給大家,運行時用這里的代碼加下面任意一個實例的代碼即可運行程序!
- privateBitmapSourceBitmap;
- privateBitmapMyBitmap;
- privatevoidbutton2_Click(objectsender,EventArgse)
- {
- //打開圖像文件
- OpenFileDialogopenFileDialog=newOpenFileDialog();
- 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=newBitmap(openFileDialog.FileName);
- //得到縮放后的圖像
- MyBitmap=newBitmap(SourceBitmap,this.pictureBox1.Width,this
- .pictureBox1.Height);
- this.pictureBox1.Image=MyBitmap;
- }
- }
一、以上下反轉(zhuǎn)的方式實現(xiàn)C#顯示圖像.
原理:計算圖像位置和高度后以高度的一半為軸進(jìn)行對換上下半邊的圖像。
代碼:
- privatevoidbutton1_Click(objectsender,EventArgse)
- {
- try
- {
- intwidth=this.MyBitmap.Width;//圖像寬度
- intheight=this.MyBitmap.Height;//圖像高度
- Graphicsg=this.panel1.CreateGraphics();
- g.Clear(Color.Gray);
- for(inti=-width/2;i<=width/2;i++)
- {
- g.Clear(Color.Gray);
- intj=Convert.ToInt32(i*(Convert.ToSingle(height)/Convert.ToS
- ingle(width)));
- RectangleDestRect=newRectangle(0,height/2-j,width,2*j);
- RectangleSrcRect=newRectangle(0,0,MyBitmap.Width,MyBitmap.Height);
- g.DrawImage(MyBitmap,DestRect,SrcRect,GraphicsUnit.Pixel);
- System.Threading.Thread.Sleep(10);
- }
- }
- catch(Exceptionex)
- {
- MessageBox.Show(ex.Message,"信息提示");
- }
- }
二、以上下對接的方式實現(xiàn)C#顯示圖像
原理:首先將圖像分為上下兩部分, 然后分別顯示。
代碼:
- privatevoidbutton1_Click(objectsender,EventArgse)
- {
- try
- {
- intwidth=this.pictureBox1.Width;//圖像寬度
- intheight=this.pictureBox1.Height;//圖像高度
- Graphicsg=this.panel1.CreateGraphics();
- g.Clear(Color.Gray);
- Bitmapbitmap=newBitmap(width,height);
- intx=0;
- while(x<=height/2)
- {
- for(inti=0;i<=width-1;i++)
- {
- bitmap.SetPixel(i,x,MyBitmap.GetPixel(i,x));
- }
- for(inti=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(Exceptionex)
- {
- MessageBox.Show(ex.Message,"信息提示");
- }
- }
三、以四周擴(kuò)散的方式顯示圖像
原理:首先設(shè)置圖像顯示的位置, 然后按高度和寬度的比例循環(huán)輸出, 直到高度和寬度為原始大小。
代碼:
- privatevoidbutton1_Click(objectsender,EventArgse)
- {
- try
- {
- intwidth=this.MyBitmap.Width;//圖像寬度
- intheight=this.MyBitmap.Height;//圖像高度
- //取得Graphics對象
- Graphicsg=this.panel1.CreateGraphics();
- g.Clear(Color.Gray);//初始為全灰色
- for(inti=0;i<=width/2;i++)
- {
- intj=Convert.ToInt32(i*(Convert.ToSingle(height)/Convert.ToSingle(width)));
- RectangleDestRect=newRectangle(width/2-i,height/2-j,2*i,2*j);
- RectangleSrcRect=newRectangle(0,0,MyBitmap.Width,MyBitmap.Height);
- g.DrawImage(MyBitmap,DestRect,SrcRect,GraphicsUnit.Pixel);
- System.Threading.Thread.Sleep(10);
- }
- }
- catch(Exceptionex)
- {
- MessageBox.Show(ex.Message,"信息提示");
- }
- }
【編輯推薦】