熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

用C#來實現以動畫的方式顯示圖像

2013-11-12 23:38:31  來源: .NET編程 

  說明

  由於是以動畫方式顯示圖像這裡沒辦法直接貼靜態截圖因此決定給園友開源將所有的可運行代碼附在案例後面由於所有的動畫處理圖像的對象放在都pictureBox控件中同時定義的類都大同小異因此這裡先把下面案例中要用到的所有類及裝載圖像的代碼給大家運行時用這裡的代碼加下面任意一個實例的代碼即可運行程序!

  private Bitmap SourceBitmap; private Bitmap MyBitmap; private void button_Click(object sender EventArgs e) { //打開圖像文件 OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialogFilter = 圖像文件(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 (openFileDialogShowDialog() == DialogResultOK) { //得到原始大小的圖像 SourceBitmap = new Bitmap(openFileDialogFileName); //得到縮放後的圖像 MyBitmap = new Bitmap(SourceBitmap thispictureBoxWidth this
pictureBoxHeight); thispictureBoxImage = MyBitmap; } }

  一以上下反轉的方式顯示圖像

  原理計算圖像位置和高度後以高度的一半為軸進行對換上下半邊的圖像

  代碼

  private void button_Click(object sender EventArgs e) { try { int width = thisMyBitmapWidth; //圖像寬度 int height = thisMyBitmapHeight; //圖像高度 Graphics g = thispanelCreateGraphics(); gClear(ColorGray); for (int i = width / ; i <= width / ; i++) { gClear(ColorGray); int j = ConvertToInt(i * (ConvertToSingle(height) / ConvertToS
ingle(width))); Rectangle DestRect = new Rectangle( height / j width * j); Rectangle SrcRect = new Rectangle( MyBitmapWidth MyBitmapHeight); gDrawImage(MyBitmap DestRect SrcRect GraphicsUnitPixel); SystemThreadingThreadSleep(); } } catch (Exception ex) { MessageBoxShow(exMessage 信息提示); } }

  二以上下對接的方式顯示圖像

  原理首先將圖像分為上下兩部分 然後分別顯示

  代碼

  private void button_Click(object sender EventArgs e) { try { int width = thispictureBoxWidth; //圖像寬度 int height = thispictureBoxHeight; //圖像高度 Graphics g = thispanelCreateGraphics(); gClear(ColorGray); Bitmap bitmap = new Bitmap(width height); int x = ; while (x <= height / ) { for (int i = ; i <= width ; i++) { bitmapSetPixel(i x MyBitmapGetPixel(i x)); } for (int i = ; i <= width ; i++) { bitmapSetPixel(i height x MyBitmapGetPixel(i height x )); } x++; thispanelRefresh(); gDrawImage (bitmap); SystemThreadingThreadSleep(); } } catch (Exception ex) { MessageBoxShow(exMessage 信息提示); } }

  三以四周擴散的方式顯示圖像

  原理首先設置圖像顯示的位置 然後按高度和寬度的比例循環輸出 直到高度和寬度為原始大小

  代碼

  

  private void button_Click(object sender EventArgs e) { try { int width = thisMyBitmapWidth; //圖像寬度 int height = thisMyBitmapHeight; //圖像高度 //取得Graphics對象 Graphics g = thispanelCreateGraphics(); gClear(ColorGray); //初始為全灰色 for (int i = ; i <= width / ; i++) { int j = ConvertToInt (i*(ConvertToSingle(height) / ConvertToSingle(width))); Rectangle DestRect = new Rectangle(width / i height/j * i *j); Rectangle SrcRect = new Rectangle( MyBitmapWidth MyBitmapHeight); gDrawImage(MyBitmap DestRect SrcRect GraphicsUnitPixel); SystemThreadingThreadSleep(); } } catch (Exception ex) { MessageBoxShow(exMessage 信息提示); } }

  四以分塊效果顯示圖像

  原理首先將圖分為幾塊 再使用 Bitmap 類的 Clone方法從原圖指定的塊中復制圖像

  最後將這些塊依次顯示出來便可

  代碼

  

  private void button_Click(object sender EventArgs e) { Graphics g = thispanelCreateGraphics(); gClear(ColorWhite); int width = MyBitmapWidth; int height = MyBitmapHeight; //定義將圖片切分成四個部分的區域 RectangleF[] block ={ new RectangleF(width/height/)

  new RectangleF(width/width/height/) new RectangleF(height/width/height/) new RectangleF(width/height/width/height/)}; //分別克隆圖片的四個部分 Bitmap[] MyBitmapBlack ={ MyBitmapClone(block[]SystemDrawingImagingPixelFormatDontCare) MyBitmapClone(block[]SystemDrawingImagingPixelFormatDontCare) MyBitmapClone(block[]SystemDrawingImagingPixelFormatDontCare) MyBitmapClone(block[]SystemDrawingImagingPixelFormatDontCare)}; //繪制圖片的四個部分各部分繪制時間間隔為秒 gDrawImage(MyBitmapBlack[] ); SystemThreadingThreadSleep(); gDrawImage(MyBitmapBlack[] width / ); SystemThreadingThreadSleep(); gDrawImage(MyBitmapBlack[] width / height / ); SystemThreadingThreadSleep(); gDrawImage(MyBitmapBlack[] height / ); }

  五以淡入淡出效果顯示圖像

  原理使用 ImageAttrributes 類的 SetColorMatrix() 方法設置顏色 調整矩陣實現淡出的效果 此類還可以對顏色進行校正 調暗 調亮和移除等

  代碼



  

  private void button_Click(object sender EventArgs e) { try { Graphics g = thispanelCreateGraphics(); gClear(ColorGray); int width = MyBitmapWidth; int height = MyBitmapHeight; ImageAttributes attributes = new ImageAttributes(); ColorMatrix matrix = new ColorMatrix(); //創建淡入顏色矩陣 matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); //從進行修改色彩變換矩陣主對角線上的數值 //使三種基准色的飽和度漸增 Single count = (float); while (count < ) { matrixMatrix = count; matrixMatrix = count; matrixMatrix = count; matrixMatrix = count; attributesSetColorMatrix(matrix ColorMatrixFlagDefault ColorAdjustTypeBitmap); gDrawImage(MyBitmap new Rectangle( width height) width height GraphicsUnitPixel attributes); private void button_Click(object sender EventArgs e) SystemThreadingThreadSleep(); count = (float)(count + ); } } catch (Exception ex) { MessageBoxShow(exMessage 信息提示); } } private void button_Click(object sender EventArgs e) { try { Graphics g = thispanelCreateGraphics(); gClear(ColorGray); int width = MyBitmapWidth; int height = MyBitmapHeight; ImageAttributes attributes = new ImageAttributes(); ColorMatrix matrix = new ColorMatrix(); //創建淡出顏色矩陣 matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); //從進行修改色彩變換矩陣主對角線上的數值 //依次減少每種色彩分量 Single count = (float); while (count > ) { matrixMatrix = (float)count; matrixMatrix = (float)count; matrixMatrix = (float)count; matrixMatrix = (float)count; attributesSetColorMatrix(matrix ColorMatrixFlagDefault ColorAdjustTypeBitmap); gDrawImage(MyBitmap new Rectangle( width height) width height GraphicsUnitPixel attributes); SystemThreadingThreadSleep(); count = (float)(count ); } } catch (Exception ex) { MessageBoxShow(exMessage 信息提示); } }

  六以左右對接的方式顯示圖像

  原理首先將圖像分為左右兩部分 然後分別顯示

  代碼

  

  private void button_Click(object sender EventArgs e) { //以左右對接方式顯示圖像 try { int width = thisMyBitmapWidth; //圖像寬度 int height = thisMyBitmapHeight; //圖像高度 Graphics g = thispanelCreateGraphics(); gClear(ColorGray); //初始為全灰色 Bitmap bitmap = new Bitmap(width height); int x = ; while (x <= width / ) { for (int i = ; i <= height ; i++) { bitmapSetPixel(x i MyBitmapGetPixel(x i)); } for (int i = ; i <= height ; i++) { bitmapSetPixel(width x i MyBitmapGetPixel(width x i)); } x++; thispanelRefresh(); gDrawImage (bitmap); SystemThreadingThreadSleep(); } } catch (Exception ex) { MessageBoxShow(exMessage 信息提示); } }

  七以左右反轉的方式顯示圖像

  原理計算圖像位置和高度後以寬度的一半為軸進行對換左右半邊的圖像

  代碼

  

  private void button_Click(object sender EventArgs e) { //以左右反轉方式顯示圖像 try { int width = thisMyBitmapWidth; //圖像寬度 int height = thisMyBitmapHeight; //圖像高度 Graphics g = thispanelCreateGraphics(); gClear(ColorGray); //初始為全灰色 for (int j = height / ; j <= height / ; j++) { gClear(ColorGray); //初始為全灰色 int i = ConvertToInt(j * (ConvertToSingle(width) / ConvertToSingle(height))); Rectangle DestRect = new Rectangle(width / i * i height); Rectangle SrcRect = new Rectangle( MyBitmapWidth MyBitmapHeight); gDrawImage(MyBitmap DestRect SrcRect GraphicsUnitPixel); SystemThreadingThreadSleep(); } } catch (Exception ex) { MessageBoxShow(exMessage 信息提示); } }

  八以從上向下拉伸的方式顯示圖像

  原理將圖像的寬度不變每次顯示圖像的一部分 直到將圖片完全顯示

  代碼

  

  private void button_Click(object sender EventArgs e) { //以從上向下拉伸方式顯示圖像 try { int width = thisMyBitmapWidth; //圖像寬度 int height = thisMyBitmapHeight; //圖像高度 Graphics g = thispanelCreateGraphics(); gClear(ColorGray); //初始為全灰色 for (int y = ; y <= height; y++) { Bitmap bitmap=MyBitmapClone (new Rectangle(width y) SystemDrawing ImagingPixelFormat FormatbppRgb ); gDrawImage (bitmap); SystemThreadingThreadSleep(); } } catch (Exception ex) { MessageBoxShow(exMessage 信息提示); } }

  九以從左向右拉伸的方式顯示圖像

  原理將圖像的高度不變每次顯示圖像的一部分 直到將圖片完全顯示

  代碼

  

  private void button_Click(object sender EventArgs e) {//以從左向右拉伸方式顯示圖像try { int width = thisMyBitmapWidth; //圖像寬度 int height = thisMyBitmapHeight; //圖像高度 Graphics g = thispanelCreateGraphics();gClear(ColorGray); //初始為全灰色 for (int x = ; x <= width; x++) { Bitmap bitmap=MyBitmapClone (new Rectangle (x height) SystemDrawing ImagingPixelFormat FormatbppRgb ); gDrawImage (bitmap); SystemThreadingThreadSleep(); } } catch (Exception ex){MessageBoxShow(exMessage 信息提示); } }

  十以任意角度旋轉圖像

  原理主要使用了 Graphics 類提供的 RotateTransform() 方法對圖像進行旋轉

  代碼

  

  private void button_Click(object sender EventArgs e) { //以任意角度旋轉顯示圖像 Graphics g = thispanelCreateGraphics(); float MyAngle = ;//旋轉的角度 while (MyAngle < ) { TextureBrush MyBrush = new TextureBrush(MyBitmap); thispanelRefresh(); MyBrushRotateTransform(MyAngle); gFillRectangle(MyBrush thisClientRectangleWidth thisClientRectangleHeight); MyAngle += f; SystemThreadingThreadSleep(); } }

  十一以橢圓的方式顯示圖像

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

  代碼

  十二以不同的透明度顯示圖像

  原理Graphics 類的 FromArgb() 方法

  代碼

  

  private void button_Click(object sender EventArgs e) { //以不同的透明度顯示圖像 Graphics g = thispanelCreateGraphics(); gSmoothingMode = SmoothingModeAntiAlias; TextureBrush MyBrush = new TextureBrush(MyBitmap); gFillRectangle(MyBrush thispanelClientRectangle); for (int i = ; i < ; i++) {//由透明變為不透明 gFillRectangle(new SolidBrush(ColorFromArgb(iColorDarkS
lateGray)) thispanelClientRectangle); SystemThreadingThreadSleep(); } }

  十三以不同分辨率顯示圖像

  原理Bitmap 類的 SetResolution 方法

  代碼

  

  private void button_Click(object sender EventArgs e) { //以不同的分辨率顯示圖像 Graphics g = thispanelCreateGraphics(); for (int i = ; i < thispanelHeight; i += ) { gClear(ColorGray); MyBitmapSetResolution(i i); gDrawImage(MyBitmap ); SystemThreadingThreadSleep(); } }

  十四以不同翻轉方式顯示圖像

  原理Bitmap 類的

  RotateFip()方法

  代碼



  

  private void button_Click(object sender EventArgs e) { //以不同翻轉方式顯示圖像 Graphics g = thispanelCreateGraphics(); for (int i = ; i < ; i++) { switch (i) { case : MyBitmapRotateFlip(RotateFlipTypeRotateNoneFlipX); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipNone); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipX); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipXY); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipY); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipNone); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipX); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipXY); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipY); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipNone); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipX); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipXY); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipY); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateNoneFlipNone); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateNoneFlipX); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateNoneFlipXY); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateNoneFlipY); break; } gClear(ColorWhite); gDrawImage(MyBitmap ); SystemThreadingThreadSleep(); } }

  

  private void button_Click(object sender EventArgs e) { //橢圓顯示圖像 thispanelRefresh(); Graphics g = thispanelCreateGraphics(); TextureBrush MyBrush = new TextureBrush(MyBitmap); gFillEllipse(MyBrush thispanelClientRectangle); }


From:http://tw.wingwit.com/Article/program/net/201311/11180.html
  • 上一篇文章:

  • 下一篇文章: 没有了
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.