天天看点

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>