c# 遮罩

本文介绍如何使用C#编程实现图像遮罩效果,包括加载图像、创建遮罩层并实现从两边向中间显示遮罩的过程,增加动态显示图像的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引用:http://lazynight.me/587.html

这节讲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 }

下载源码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值