110,580
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace FormTest1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<XImage> _list; //用来存储需要淡入淡出的图
bool _isChanged = false; //记录是否已变化过一幅图,此简单示例中仅仅两幅图,所以只需一个bool记录即可,若为多幅,可以设定int型来指示
XImage _ximg; //当前正在淡入淡出的图
int _count; //当前淡入淡出播放到第几帧
private void Form1_Load(object sender, EventArgs e)
{
_list = new List<XImage>();
//解决方案管理器那里,项目的Properies下面有个Resources.resx,双击打开后
//“添加资源”菜单左边,默认是“字符串”菜单,这个“字符串”菜单可以下拉选择,更改为“图像”
//然后点击“添加资源”的下拉表,选择“添加现有文件”,即可选择一幅图像添加进来
//添加之后,选中图像,右侧选择“属性”显示,可以看到其属性设置
//当然,你也可以使用Image.FromFile方法加载图像
//这个示例为了简单化,两幅图都是128*128的,pictureBox1的设置也是与之匹配的,请根据自己的实际修改pictureBox1的参数设置
_list.Add(new XImage(global::FormTest1.Properties.Resources.a17));
_list.Add(new XImage(global::FormTest1.Properties.Resources.a26));
_ximg = _list[0];
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; //自动放大缩小图片使其符合pictureBox1的宽和高
}
/// <summary>
/// 这个是定时器定时执行的方法,目前设定为200毫秒一次
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
if (!_isChanged)
{
//还没有播放完第一幅图,第一幅是慢慢消失
if (_count < _ximg.Count)
{
//播放第一幅图的当前帧
pictureBox1.Image = _ximg.GetIamge(_count);
//计数加1,下一次执行此方法时播放的就是下一帧了
_count++;
}
else
{
//已经计数到了最大值
//换下一幅图
_ximg = _list[1];
_isChanged = true;
}
}
else if (_count > 0)
{
//此时在播放第二副图,起始_count值是最大值,需要减1之后做索引正好实现慢慢出现的效果
_count--;
pictureBox1.Image = _ximg.GetIamge(_count);
}
else
{
//两幅都播放完了,定时器不再工作
timer1.Enabled = false;
}
}
/// <summary>
/// 这个是再来一次按钮的功能,初始化参数,重新启用定时器,则再来一遍。Have fun!
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
_ximg = _list[0];
_count = 0;
_isChanged = false;
timer1.Enabled = true;
}
}
public class XImage
{
List<Image> _list = new List<Image>(); //用来存储原始图和基于原始图的各种透明度的图片
/// <summary>
/// 原始图加上透明图的总数目
/// </summary>
public int Count
{
get { return _list.Count; }
}
public XImage() { ;}
public XImage(Image img)
{
SetImage(img);
}
/// <summary>
/// 将原始图和基于原始图获得的一系列透明图存储起来,存储在_list中
/// </summary>
/// <param name="img">原始图</param>
public void SetImage(Image img)
{
_list.Clear(); //先清空原先的内容
Bitmap bmp = new Bitmap(img); //基于原始图生成一个新的bmp,因为原始图可能是jpg,png,gif等
_list.Add(bmp); //首先加入原始图,也就是说第0副是原始图
//因为第0副是原始图,所以从1开始循环
for (int i = 1; i < 11;i++ )
{
Bitmap tmpBmp = SetAlpha(bmp, (int)(255 * (1 - 0.1 * i)));
_list.Add(tmpBmp);
}
}
public Image GetIamge(int index)
{
return _list[index];
}
/// <summary>
/// 此方法处理一副bmp图,按照设定的“不透明度”,为其生成一副新的bmp图
/// </summary>
/// <param name="bmp">原始图</param>
/// <param name="alpha">设定的alpha度,也即不透明度,0为完全透明,255为最大值,完全显示,一点都不透明</param>
/// <returns>基于参数bmp和alpha值得到的新图</returns>
private Bitmap SetAlpha(Bitmap bmp, int alpha)
{
Color col;
Bitmap bmp2 = new Bitmap(bmp);
for (int i = 0; i < bmp.Width; i++)
for (int j = 0; j < bmp.Height; j++)
{
col = bmp.GetPixel(i, j);
if (col.A > 0)
bmp2.SetPixel(i, j, Color.FromArgb(alpha, col.R, col.G, col.B)); //ARGB A即alpha度,R为red,G为green,B为blue
}
return bmp2;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FormTest1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<XImage> _list;
bool _isChanged = false;
XImage _ximg;
int _count;
private void Form1_Load(object sender, EventArgs e)
{
_list = new List<XImage>();
_list.Add(new XImage(global::FormTest1.Properties.Resources.a17));
_list.Add(new XImage(global::FormTest1.Properties.Resources.a26));
_ximg = _list[0];
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (!_isChanged)
{
if (_count < _ximg.Count)
{
pictureBox1.Image = _ximg.GetIamge(_count);
_count++;
}
else
{
_ximg = _list[1];
_isChanged = true;
}
}
else if (_count > 0)
{
_count--;
pictureBox1.Image = _ximg.GetIamge(_count);
}
else
{
timer1.Enabled = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
_ximg = _list[0];
_count = 0;
_isChanged = false;
timer1.Enabled = true;
}
}
public class XImage
{
List<Image> _list = new List<Image>();
public int Count
{
get { return _list.Count; }
}
public XImage() { ;}
public XImage(Image img)
{
SetImage(img);
}
public void SetImage(Image img)
{
_list.Clear();
Bitmap bmp = new Bitmap(img);
_list.Add(bmp);
for (int i = 1; i < 11;i++ )
{
Bitmap tmpBmp = SetAlpha(bmp, (int)(255 * (1 - 0.1 * i)));
_list.Add(tmpBmp);
}
}
public Image GetIamge(int index)
{
return _list[index];
}
private Bitmap SetAlpha(Bitmap bmp, int alpha)
{
Color col;
Bitmap bmp2 = new Bitmap(bmp);
for (int i = 0; i < bmp.Width; i++)
for (int j = 0; j < bmp.Height; j++)
{
col = bmp.GetPixel(i, j);
if (col.A > 0)
bmp2.SetPixel(i, j, Color.FromArgb(alpha, col.R, col.G, col.B));
}
return bmp2;
}
}
}
namespace FormTest1
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 200;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(115, 39);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(128, 128);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(140, 220);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "再来一遍";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(374, 292);
this.Controls.Add(this.button1);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button button1;
}
}