天天看點

c# 遮罩

這節講c#的圖像遮罩效果。圖像被一個移動的遮罩層擋住,然後遮罩由兩邊向中間顯示,直到完全顯示出圖像。增加顯示圖像的動态效果。

c# 遮罩

001 /*

002 * 由SharpDevelop建立。

003 * 使用者: Lazynight

004 * 日期: 2011/10/22

005 * 時間: 6:59

006 * 

007 * 要改變這種模闆請點選 工具|選項|代碼編寫|編輯标準頭檔案

008 */

009 using System;

010 using System.Collections.Generic;

011 using System.Drawing;

012 using System.Windows.Forms;

013 using System.ComponentModel;

014

015 namespace Lazy20_圖像的遮罩效果

016 {

017     public partial class MainForm : Form

018     {

019         Bitmap Night_bitmap;//水準遮罩,垂直遮罩公用變量

020         public MainForm()

021         {

022             InitializeComponent();

023         }

024

025         void Button1Click(object sender, EventArgs e)

026         {

027             openFileDialog1.Filter= “*.jpg,*.jpeg,*.bmp,*.gif,*.ico,*.png,*.tif,*.wmf|*.jpg;*.jpeg;*.bmp;*.gif;*.ico;*.png;*.tif;*.wmf”;

028             //設定打開圖像的類型

029             this.openFileDialog1.ShowDialog();//打開對話框

030             if(this.openFileDialog1.FileName.Trim()==“”)

031                 return;

032             try

033             {

034                 Bitmap Lazy_bitmap=new Bitmap(this.openFileDialog1.FileName);

035                 //得到原始大小的圖像

036                 Night_bitmap=new Bitmap(Lazy_bitmap,this.pictureBox1.Width,this.pictureBox1.Height);

037                 this.pictureBox1.Image=Night_bitmap;

038             }

039             catch(Exception Err)

040             {

041                 MessageBox.Show(this,“打開圖像檔案錯誤”,“提示”,MessageBoxButtons.OK,MessageBoxIcon.Information);

042             }

043         }

044

045         void Button2Click(object sender, EventArgs e)

046         {

047             int Lazy_width=this.pictureBox1.Width;//圖像寬度 

048             int Lazy_height=this.pictureBox1.Height;//圖像高度

049             Graphics Lazy_pic =this.pictureBox1.CreateGraphics();

050             //擷取Graphics對象

051             Lazy_pic.Clear(Color.Red);//初始為紅色

052             Bitmap Lazy_bitmap=new Bitmap(Lazy_width,Lazy_height);

053             int x=0;

054             while(x<=Lazy_width/2)

055             {

056                 for(int i=0;i<=Lazy_height-1;i++)

057                 {

058                     Lazy_bitmap.SetPixel(x,i,Night_bitmap.GetPixel(x,i));

059                     //上半部分自上而下遮蓋,執行到圖像一半

060                 }

061                 for(int i=0;i<=Lazy_height-1;i++)

062                 {

063                     Lazy_bitmap.SetPixel(Lazy_width-x-1,i,Night_bitmap.GetPixel(Lazy_width-x-1,i));

064                     //下半部分自下而上遮蓋,執行到圖像一半

065                 }

066                 x++;

067                 this.pictureBox1.Refresh();//圖像重新整理

068                 this.pictureBox1.Image=Lazy_bitmap;

069                 System.Threading.Thread.Sleep(30);//通過挂起線程時間來控制遮蓋的速度,此處為睡眠30/1000毫秒

070             }

071         }

072

073         void Button3Click(object sender, EventArgs e)

074         {

075             int Lazy_width=this.pictureBox1.Width;//圖像寬度 

076             int Lazy_height=this.pictureBox1.Height;//圖像高度

077             Graphics Lazy_pic =this.pictureBox1.CreateGraphics();

078             //擷取Graphics對象

079             Lazy_pic.Clear(Color.Red);//初始為紅色

080             Bitmap Lazy_bitmap=new Bitmap(Lazy_width,Lazy_height);

081             int x=0;

082             while(x<=Lazy_height/2)

083             {

084                 for(int i=0;i<=Lazy_width-1;i++)

085                 {

086                     Lazy_bitmap.SetPixel(i,x,Night_bitmap.GetPixel(i,x));

087                     //上半部分自上而下遮蓋,執行到圖像一半

088                 }

089                 for(int i=0;i<=Lazy_width-1;i++)

090                 {

091                     Lazy_bitmap.SetPixel(i,Lazy_height-x-1,Night_bitmap.GetPixel(i,Lazy_height-x-1));

092                     //下半部分自下而上遮蓋,執行到圖像一半

093                 }

094                 x++;

095                 this.pictureBox1.Refresh();//圖像重新整理

096                 this.pictureBox1.Image=Lazy_bitmap;

097                 System.Threading.Thread.Sleep(30);//通過挂起線程時間來控制遮蓋的速度,此處為睡眠30/1000毫秒

098

099             }

100         }

101     }

102 }

<a href="http://down.qiannao.com/space/file/flowerowl/-4e0a-4f20-5206-4eab/Lazy20_-56fe-50cf-7684-906e-7f69-6548-679c.rar/.page" target="_blank">下載下傳源碼</a>