如何实现是一副bitmap淡入淡出 如何实现一个bitmap在窗体上显示淡入淡出的效果!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 参考这里:http://soft.ccw.com.cn/programing/dotnet/htm2008/20080926_510490.shtml 尤其这一段: 五. 以淡入淡出效果显示图像 原理: 使用 ImageAttrributes 类的 SetColorMatrix() 方法设置颜色, 调整矩阵实现淡出的效果. 此类还可以对颜色进行校正, 调暗, 调亮和移除等. 代码: private void button1_Click(object sender, EventArgs e) { try { Graphics g = this.panel1.CreateGraphics(); g.Clear(Color.Gray); int width = MyBitmap.Width; int height = MyBitmap.Height; ImageAttributes attributes = new ImageAttributes(); ColorMatrix matrix = new ColorMatrix(); //创建淡入颜色矩阵 matrix.Matrix00 = (float)0.0; matrix.Matrix01 = (float)0.0; matrix.Matrix02 = (float)0.0; matrix.Matrix03 = (float)0.0; matrix.Matrix04 = (float)0.0; matrix.Matrix10 = (float)0.0; matrix.Matrix11 = (float)0.0; matrix.Matrix12 = (float)0.0; matrix.Matrix13 = (float)0.0; matrix.Matrix14 = (float)0.0; matrix.Matrix20 = (float)0.0; matrix.Matrix21 = (float)0.0; matrix.Matrix22 = (float)0.0; matrix.Matrix23 = (float)0.0; matrix.Matrix24 = (float)0.0; matrix.Matrix30 = (float)0.0; matrix.Matrix31 = (float)0.0; matrix.Matrix32 = (float)0.0; matrix.Matrix33 = (float)0.0; matrix.Matrix34 = (float)0.0; matrix.Matrix40 = (float)0.0; matrix.Matrix41 = (float)0.0; matrix.Matrix42 = (float)0.0; matrix.Matrix43 = (float)0.0; matrix.Matrix44 = (float)0.0; matrix.Matrix33 = (float)1.0; matrix.Matrix44 = (float)1.0; //从0到1进行修改色彩变换矩阵主对角线上的数值 //使三种基准色的饱和度渐增 Single count = (float)0.0; while (count < 1.0) { matrix.Matrix00 = count; matrix.Matrix11 = count; matrix.Matrix22 = count; matrix.Matrix33 = count; attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); g.DrawImage(MyBitmap, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel, attributes);System.Threading.Thread.Sleep(200); count = (float)(count + 0.02); } } catch (Exception ex) { MessageBox.Show(ex.Message, "信息提示"); } } private void button3_Click(object sender, EventArgs e) { try { Graphics g = this.panel1.CreateGraphics(); g.Clear(Color.Gray); int width = MyBitmap.Width; int height = MyBitmap.Height; ImageAttributes attributes = new ImageAttributes(); ColorMatrix matrix = new ColorMatrix(); //创建淡出颜色矩阵 matrix.Matrix00 = (float)0.0; matrix.Matrix01 = (float)0.0; matrix.Matrix02 = (float)0.0; matrix.Matrix03 = (float)0.0; matrix.Matrix04 = (float)0.0; matrix.Matrix10 = (float)0.0; matrix.Matrix11 = (float)0.0; matrix.Matrix12 = (float)0.0; matrix.Matrix13 = (float)0.0; matrix.Matrix14 = (float)0.0; matrix.Matrix20 = (float)0.0; matrix.Matrix21 = (float)0.0; matrix.Matrix22 = (float)0.0; matrix.Matrix23 = (float)0.0; matrix.Matrix24 = (float)0.0; matrix.Matrix30 = (float)0.0; matrix.Matrix31 = (float)0.0; matrix.Matrix32 = (float)0.0; matrix.Matrix33 = (float)0.0; matrix.Matrix34 = (float)0.0; matrix.Matrix40 = (float)0.0; matrix.Matrix41 = (float)0.0; matrix.Matrix42 = (float)0.0; matrix.Matrix43 = (float)0.0; matrix.Matrix44 = (float)0.0; matrix.Matrix33 = (float)1.0; matrix.Matrix44 = (float)1.0; //从1到0进行修改色彩变换矩阵主对角线上的数值 //依次减少每种色彩分量 Single count = (float)1.0; while (count > 0.0) { matrix.Matrix00 = (float)count; matrix.Matrix11 = (float)count; matrix.Matrix22 = (float)count; matrix.Matrix33 = (float)count; attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); g.DrawImage(MyBitmap, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel, attributes); System.Threading.Thread.Sleep(20); count = (float)(count - 0.01); } } catch (Exception ex) { MessageBox.Show(ex.Message, "信息提示"); } } 提到AnimateWindows这里给一个我之前看到的很好的代码:http://www.codeproject.com/KB/cs/FormAnimation.aspx代码很实用,但不知道是否是你想要的Bitmap的淡入淡出效果:) 3楼的方法差不多了,需要用ColorMatrix。ColorMatrix定义包含 RGBA 空间坐标的 5 x 5 矩阵。ImageAttributes 类的若干方法通过使用颜色矩阵调整图像颜色。矩阵系数组成一个 5 x 5 的线性转换,用于转换 ARGB 单色值。例如,ARGB 向量表示为 Alpha、Red(红色)、Green(绿色)、Blue(蓝色)和 W,此处 W 始终为 1。 另外,3楼我帖的代码刚才测试了一下,可用。但需要做以下几个处理:1、添加一个pannel1的对象;2、在窗口类中声明一个private Bitmap MyBitmap,并且在调用淡入淡出代码之前初始化它,比如:MyBitmap = new Bitmap("文件名称");3、测试用的文件别太大,大文件会很慢、有波纹、闪烁并且会导致停止响应;针对第三点可能需要进行适当的优化才能实用。 private void button3_Click(object sender, EventArgs e) { Graphics g = this.CreateGraphics(); ColorMatrix myColorMatrix = new ColorMatrix(); myColorMatrix.Matrix33 = 0.5f; ImageAttributes imageAttr = new ImageAttributes(); imageAttr.SetColorMatrix(myColorMatrix); Rectangle rect = new Rectangle(50, 20, 300, 300); Graphics.FromImage(btco).DrawImage(bt, rect, 0, 0, 300, 300, GraphicsUnit.Pixel, imageAttr); g.DrawImage(btco, 20, 20); }我郁闷了。我这样画出的图片并不是一下子就显示在窗体上的。而是自上而下的逐行扫描到窗体上的。大家可以把我的代码复制到自己的程序中看看就知道。很是郁闷!!! 淡入淡出的实现原理是设置图片的透明度,从透明到不透明,再从不透明到透明。以下是我在C++中用的设置图片透明度的方法,变成C#就可以用了。ColorMatrix matrix; for( int i = 0; i < 5; i++ ) for( int j = 0; j < 5; j++ ) matrix.m[i][j] = (i == j ?1.0f : 0.0fmatrix.m[3][3] = opacity; //透明度 浮点数,0.0到1.0ImageAttributes imageAttr;imageAttr.SetColorMatrix( &matrix ); g.DrawImage( backimg, rect, -si1.cx, -si1.cy, si2.cx, si2.cy, UnitPixel, &imageAttr1 ); 我这个代码就是双缓冲呀。我就是先把他画到了我建立的一个bitmap上。然后又把bitmap一次显示到桌面上去的。但是还是有波纹!!! 这个貌似只能用于窗口。不能用于panel把!!! using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Drawing.Imaging;namespace ImageFlash{ public class AnimateControl : Control { private Image _image; private System.Threading.Timer _timer; private static readonly int Interval = 100; private bool _stop = true; private bool _flashIn = true; private float _opaque; private bool _flash; private float _step = 0.02f; public AnimateControl() { SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true); } [DefaultValue(null)] public Image Image { get { return _image; } set { if (!_stop) throw new Exception("Flashing"); _image = value; if (DesignMode) Invalidate(); } } [DefaultValue(true)] public bool FlashIn { get { return _flashIn; } set { if (!_stop) throw new Exception("Flashing"); _flashIn = value; } } [DefaultValue(0.02f)] public float Step { get { return _step; } set { if (!_stop) throw new Exception("Flashing"); if (value < 0) _step = 0.01f; else if (value > 1) _step = 1f; else _step = value; } } private System.Threading.Timer Timer { get { if (_timer == null) { _timer = new System.Threading.Timer( new System.Threading.TimerCallback(Animate), null, -1, Interval); } return _timer; } } protected override void OnPaint(PaintEventArgs pe) { base.OnPaint(pe); if (!DesignMode && _flash) { AnimateDraw(pe.Graphics, _opaque); } else if (DesignMode) { if (_image != null) pe.Graphics.DrawImage( _image, 0, 0); } } private void AnimateDraw(Graphics g, float op) { if (_image == null) return; float[][] matrix = new float[][]{ new float[]{op,0,0,0,0}, new float[]{0,op,0,0,0}, new float[]{0,0,op,0,0}, new float[]{0,0,0,op,0}, new float[]{0,0,0,0,1.0f}}; ImageAttributes attributes = new ImageAttributes(); ColorMatrix colorMatrix = new ColorMatrix(matrix); int width = _image.Width; int height = _image.Height; attributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); g.DrawImage( _image, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel, attributes); } private void Animate(object obj) { bool exit = false; if (_flashIn) { _opaque += _step; if (_opaque > 1) { _opaque = 1f; exit = true; } } else { _opaque -= _step; if (_opaque < 0) { _opaque = 0f; exit = true; } } Invalidate(); if (exit) { _timer.Change(-1, Interval); _stop = true; } } public void StartFlash() { if (!_stop) return; _flash = true; _stop = false; _opaque = _flashIn ? 0f : 1f; Timer.Change(0, Interval); } public void StopFlash() { if (!_stop) { Timer.Change(-1, Interval); _stop = true; _flash = false; } } protected override Size DefaultSize { get { return new Size(200,200); } } protected override void Dispose(bool disposing) { base.Dispose(disposing); if (disposing) { if (_timer != null) { _timer.Dispose(); _timer = null; } if (_image != null) { _image.Dispose(); _image = null; } } } }} dataGridView的数据更新问题. 当前上下文中不存在名称“Request”? 大家过年好!关于安装部署打包的一个问题 如何存储checkedlistbox内容 高分--求asp.net源码,小型或中型网站的,仅供学习用 Combobox根据条件选中项问题。 关于类的地址 网站发布到外网时发生错误 怎样将textBox1清空? 请问各位,用发布向导发布到远程服务器的程序怎么包含DLL文件? 或者怎么注册DLL文件? 用过XtraGird的在虾,进来帮忙翻译一下,XtrGrid demo中各个例子的功能,分不够可以再加 请教gridview字体变大的问题
http://soft.ccw.com.cn/programing/dotnet/htm2008/20080926_510490.shtml
五. 以淡入淡出效果显示图像
原理: 使用 ImageAttrributes 类的 SetColorMatrix() 方法设置颜色, 调整矩阵实现淡出的效果. 此类还可以对颜色进行校正, 调暗, 调亮和移除等.
代码:
private void button1_Click(object sender, EventArgs e)
{
try
{
Graphics g = this.panel1.CreateGraphics();
g.Clear(Color.Gray);
int width = MyBitmap.Width;
int height = MyBitmap.Height;
ImageAttributes attributes = new ImageAttributes();
ColorMatrix matrix = new ColorMatrix();
//创建淡入颜色矩阵
matrix.Matrix00 = (float)0.0;
matrix.Matrix01 = (float)0.0;
matrix.Matrix02 = (float)0.0;
matrix.Matrix03 = (float)0.0;
matrix.Matrix04 = (float)0.0;
matrix.Matrix10 = (float)0.0;
matrix.Matrix11 = (float)0.0;
matrix.Matrix12 = (float)0.0;
matrix.Matrix13 = (float)0.0;
matrix.Matrix14 = (float)0.0;
matrix.Matrix20 = (float)0.0;
matrix.Matrix21 = (float)0.0;
matrix.Matrix22 = (float)0.0;
matrix.Matrix23 = (float)0.0;
matrix.Matrix24 = (float)0.0;
matrix.Matrix30 = (float)0.0;
matrix.Matrix31 = (float)0.0;
matrix.Matrix32 = (float)0.0;
matrix.Matrix33 = (float)0.0;
matrix.Matrix34 = (float)0.0;
matrix.Matrix40 = (float)0.0;
matrix.Matrix41 = (float)0.0;
matrix.Matrix42 = (float)0.0;
matrix.Matrix43 = (float)0.0;
matrix.Matrix44 = (float)0.0;
matrix.Matrix33 = (float)1.0;
matrix.Matrix44 = (float)1.0;
//从0到1进行修改色彩变换矩阵主对角线上的数值
//使三种基准色的饱和度渐增
Single count = (float)0.0;
while (count < 1.0)
{
matrix.Matrix00 = count;
matrix.Matrix11 = count;
matrix.Matrix22 = count;
matrix.Matrix33 = count;
attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(MyBitmap, new Rectangle(0, 0, width, height),
0, 0, width, height, GraphicsUnit.Pixel, attributes);
System.Threading.Thread.Sleep(200);
count = (float)(count + 0.02);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示");
}
}
private void button3_Click(object sender, EventArgs e)
{
try
{
Graphics g = this.panel1.CreateGraphics();
g.Clear(Color.Gray);
int width = MyBitmap.Width;
int height = MyBitmap.Height;
ImageAttributes attributes = new ImageAttributes();
ColorMatrix matrix = new ColorMatrix();
//创建淡出颜色矩阵
matrix.Matrix00 = (float)0.0;
matrix.Matrix01 = (float)0.0;
matrix.Matrix02 = (float)0.0;
matrix.Matrix03 = (float)0.0;
matrix.Matrix04 = (float)0.0;
matrix.Matrix10 = (float)0.0;
matrix.Matrix11 = (float)0.0;
matrix.Matrix12 = (float)0.0;
matrix.Matrix13 = (float)0.0;
matrix.Matrix14 = (float)0.0;
matrix.Matrix20 = (float)0.0;
matrix.Matrix21 = (float)0.0;
matrix.Matrix22 = (float)0.0;
matrix.Matrix23 = (float)0.0;
matrix.Matrix24 = (float)0.0;
matrix.Matrix30 = (float)0.0;
matrix.Matrix31 = (float)0.0;
matrix.Matrix32 = (float)0.0;
matrix.Matrix33 = (float)0.0;
matrix.Matrix34 = (float)0.0;
matrix.Matrix40 = (float)0.0;
matrix.Matrix41 = (float)0.0;
matrix.Matrix42 = (float)0.0;
matrix.Matrix43 = (float)0.0;
matrix.Matrix44 = (float)0.0;
matrix.Matrix33 = (float)1.0;
matrix.Matrix44 = (float)1.0;
//从1到0进行修改色彩变换矩阵主对角线上的数值
//依次减少每种色彩分量
Single count = (float)1.0;
while (count > 0.0)
{
matrix.Matrix00 = (float)count;
matrix.Matrix11 = (float)count;
matrix.Matrix22 = (float)count;
matrix.Matrix33 = (float)count;
attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(MyBitmap, new Rectangle(0, 0, width, height),
0, 0, width, height, GraphicsUnit.Pixel, attributes);
System.Threading.Thread.Sleep(20);
count = (float)(count - 0.01);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示");
}
}
http://www.codeproject.com/KB/cs/FormAnimation.aspx代码很实用,但不知道是否是你想要的Bitmap的淡入淡出效果:)
矩阵系数组成一个 5 x 5 的线性转换,用于转换 ARGB 单色值。例如,ARGB 向量表示为 Alpha、Red(红色)、Green(绿色)、Blue(蓝色)和 W,此处 W 始终为 1。
但需要做以下几个处理:
1、添加一个pannel1的对象;
2、在窗口类中声明一个private Bitmap MyBitmap,并且在调用淡入淡出代码之前初始化它,比如:
MyBitmap = new Bitmap("文件名称");
3、测试用的文件别太大,大文件会很慢、有波纹、闪烁并且会导致停止响应;针对第三点可能需要进行适当的优化才能实用。
{
Graphics g = this.CreateGraphics();
ColorMatrix myColorMatrix = new ColorMatrix();
myColorMatrix.Matrix33 = 0.5f;
ImageAttributes imageAttr = new ImageAttributes();
imageAttr.SetColorMatrix(myColorMatrix); Rectangle rect = new Rectangle(50, 20, 300, 300);
Graphics.FromImage(btco).DrawImage(bt, rect, 0, 0, 300, 300,
GraphicsUnit.Pixel, imageAttr);
g.DrawImage(btco, 20, 20); }
我郁闷了。我这样画出的图片并不是一下子就显示在窗体上的。而是自上而下的逐行扫描到窗体上的。大家可以把我的代码复制到自己的程序中看看就知道。很是郁闷!!!
以下是我在C++中用的设置图片透明度的方法,变成C#就可以用了。ColorMatrix matrix;
for( int i = 0; i < 5; i++ )
for( int j = 0; j < 5; j++ )
matrix.m[i][j] = (i == j ?1.0f : 0.0fmatrix.m[3][3] = opacity; //透明度 浮点数,0.0到1.0ImageAttributes imageAttr;
imageAttr.SetColorMatrix( &matrix );
g.DrawImage( backimg, rect, -si1.cx, -si1.cy, si2.cx, si2.cy, UnitPixel, &imageAttr1 );
这个貌似只能用于窗口。不能用于panel把!!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;namespace ImageFlash
{
public class AnimateControl : Control
{
private Image _image;
private System.Threading.Timer _timer;
private static readonly int Interval = 100;
private bool _stop = true;
private bool _flashIn = true;
private float _opaque;
private bool _flash;
private float _step = 0.02f; public AnimateControl()
{
SetStyle(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.ResizeRedraw |
ControlStyles.OptimizedDoubleBuffer, true);
} [DefaultValue(null)]
public Image Image
{
get { return _image; }
set
{
if (!_stop)
throw new Exception("Flashing");
_image = value;
if (DesignMode)
Invalidate();
}
} [DefaultValue(true)]
public bool FlashIn
{
get { return _flashIn; }
set
{
if (!_stop)
throw new Exception("Flashing");
_flashIn = value;
}
} [DefaultValue(0.02f)]
public float Step
{
get { return _step; }
set
{
if (!_stop)
throw new Exception("Flashing");
if (value < 0)
_step = 0.01f;
else if (value > 1)
_step = 1f;
else
_step = value;
}
} private System.Threading.Timer Timer
{
get
{
if (_timer == null)
{
_timer = new System.Threading.Timer(
new System.Threading.TimerCallback(Animate),
null,
-1,
Interval);
}
return _timer;
}
} protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe); if (!DesignMode && _flash)
{
AnimateDraw(pe.Graphics, _opaque);
}
else if (DesignMode)
{
if (_image != null)
pe.Graphics.DrawImage(
_image,
0,
0);
}
} private void AnimateDraw(Graphics g, float op)
{
if (_image == null)
return; float[][] matrix = new float[][]{
new float[]{op,0,0,0,0},
new float[]{0,op,0,0,0},
new float[]{0,0,op,0,0},
new float[]{0,0,0,op,0},
new float[]{0,0,0,0,1.0f}};
ImageAttributes attributes = new ImageAttributes();
ColorMatrix colorMatrix = new ColorMatrix(matrix); int width = _image.Width;
int height = _image.Height; attributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(
_image,
new Rectangle(0, 0, width, height),
0,
0,
width,
height,
GraphicsUnit.Pixel,
attributes);
} private void Animate(object obj)
{
bool exit = false;
if (_flashIn)
{
_opaque += _step;
if (_opaque > 1)
{
_opaque = 1f;
exit = true;
}
}
else
{
_opaque -= _step;
if (_opaque < 0)
{
_opaque = 0f;
exit = true;
}
} Invalidate();
if (exit)
{
_timer.Change(-1, Interval);
_stop = true;
}
} public void StartFlash()
{
if (!_stop)
return;
_flash = true;
_stop = false;
_opaque = _flashIn ? 0f : 1f;
Timer.Change(0, Interval);
} public void StopFlash()
{
if (!_stop)
{
Timer.Change(-1, Interval);
_stop = true;
_flash = false;
}
} protected override Size DefaultSize
{
get
{
return new Size(200,200);
}
} protected override void Dispose(bool disposing)
{
base.Dispose(disposing); if (disposing)
{
if (_timer != null)
{
_timer.Dispose();
_timer = null;
} if (_image != null)
{
_image.Dispose();
_image = null;
}
}
}
}
}