C# 做图片的一些特效处理

lyd5233890 2009-11-15 02:18:19
我想做一个程序具体功能如下:在一个文件夹里有一些图片,我想用程序让这些图片在form窗体中显示,并且图片与图片之间有一些特效的转换。比如:第一张图片先显示然后这张图片逐渐变淡直至消失后第二张图片再由淡变深的逐渐显示出来,效果也就类似于powerpoint播放的幻灯片一样。现在还不知道该怎么去实现,特向高手们请教指点一下!最好是用代码举例实现。深表感谢!
...全文
1507 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jijiyangjie 2011-07-22
  • 打赏
  • 举报
回复
FormTest1是什么东东?请问
byy19631028 2010-03-04
  • 打赏
  • 举报
回复
我也试了,这样子如果图片很大的话,就特别慢。还是改用ColorMatrix矩阵会好些,而且简单 方便
天乐 2009-11-18
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 lyd5233890 的回复:]
不错,这次运行成功了。但是,还有一个小问题,就是在运行的时候,form窗体等很长时间才显示,这是什么原因呀,是程序的原因还是我电脑硬件的原因呀。是不是还与所加载的图像的大小有关系呀?
[/Quote]

图片很大的话,肯定会慢些。你看SetAlpha这个函数,遍历访问了每一个图片的像素。

实际项目中,肯定要预先用ps、fw等图像处理工具处理一下图的,把图处理到合适的大小。
bcl258586301 2009-11-18
  • 打赏
  • 举报
回复
路过,学习...
lyd5233890 2009-11-18
  • 打赏
  • 举报
回复
不错,这次运行成功了。但是,还有一个小问题,就是在运行的时候,form窗体等很长时间才显示,这是什么原因呀,是程序的原因还是我电脑硬件的原因呀。是不是还与所加载的图像的大小有关系呀?
lyd5233890 2009-11-16
  • 打赏
  • 举报
回复
#5楼
我做的这个就是winform的。
你给我的代码我看了,但是可能是我水平太低了吧有点看不懂,你能不能在代码后面注释上相应的功能。而且如果我现在就想直接在Visual Studio 2005里面运行你给我的代码 _list.Add(new XImage(global::FormTest1.Properties.Resources.a17));
_list.Add(new XImage(global::FormTest1.Properties.Resources.a26));
以上两句报错,而且我应该源图片信息的文件夹放到哪里呀?请指教!
天乐 2009-11-16
  • 打赏
  • 举报
回复
呵呵,我的是vs2008的,项目修改为.NET2.0了,在2005下面应该没问题。
Disgner.cs就不贴了,同上。

FormTest1.cs:

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;
}
}
}

天乐 2009-11-15
  • 打赏
  • 举报
回复
如果是WinForm的,手上有一个小例子,贴给你吧

Form1.cs:


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;
}
}
}



Form1.Disgner.cs:


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;
}
}

TTOJJ 2009-11-15
  • 打赏
  • 举报
回复
好简单的了~~加个时钟触发事件改变透明度就好

玻璃按钮等效果就是这样的
天乐 2009-11-15
  • 打赏
  • 举报
回复
楼主这个是web的还是winform的?
wuyq11 2009-11-15
  • 打赏
  • 举报
回复
在web通过JS切换图片很容易显示。WINFORM中通过Timer结合imagelist显示
参考
http://topic.csdn.net/u/20081212/11/55df661b-04d4-4724-8a1d-c04675c47d17.html
woshigexiaonvren 2009-11-15
  • 打赏
  • 举报
回复
在JS中改变图片的透明度就行了

110,580

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧