public partial class ImageCarouselDevelopExt : Control
{
#region
///
/// 动画播放定时器
///
private Timer carouselTimer = new Timer();
///
/// 轮播的五个PictureBox
///
private List
///
/// 轮播的五个PictureBox位置索引
///
private List<int> carouselPictureBoxIndex = new List<int>() { 0, 1, 2, 3, 4 };
///
/// 处理后的图片
///
private List
///
/// 图片轮播的时间间隔累计(-1为动画正在切换中)
///
private int intervalTimeValue = 0;
private AnimationTimer _Animation;
#endregion
#region
private bool isReflection = false;
///
/// 是否添加倒影
///
[DefaultValue(false)]
[Description("是否添加倒影")]
public bool IsReflection
{
get { return this.isReflection; }
set { this.isReflection = value; }
}
private List
///
/// 要播放的图片
///
[Description("要播放的图片")]
public List
{
get { return this.carouselImages; }
set
{
this.carouselImages = value;
this.Reset();
}
}
private double animatiOnTime= 300;
///
/// 动画播放的总时间
///
[DefaultValue(300)]
[Description("动画播放的总时间(默认300毫秒)")]
public double AnimationTime
{
get { return this.animationTime; }
set
{
this.animatiOnTime= value;
this.Reset();
}
}
private int intervalTime = 1000;
///
/// 图片轮播的时间间隔
///
[DefaultValue(1000)]
[Description("图片轮播的时间间隔(默认1000毫秒)")]
public int IntervalTime
{
get { return this.intervalTime; }
set
{
this.intervalTime = value;
this.Reset();
}
}
#endregion
public ImageCarouselDevelopExt()
{
InitializeComponent();
this.Controls.Add(carouselPictureBox[0]);
this.Controls.Add(carouselPictureBox[1]);
this.Controls.Add(carouselPictureBox[2]);
this.Controls.Add(carouselPictureBox[3]);
this.Controls.Add(carouselPictureBox[4]);
this.carouselTimer.Interval = 50;
this.carouselTimer.Tick += new EventHandler(carouselTimer_Tick);
this._Animation = new AnimationTimer(this, new AnimationOptions());
this._Animation.AnimationIng += new AnimationTimer.AnimationHandel(Animation_AnimationIng);
this._Animation.AnimationEnding += new AnimationTimer.AnimationHandel(Animation_AnimationEnding);
}
///
/// 定时器
///
///
///
private void carouselTimer_Tick(object sender, EventArgs e)
{
if (this.intervalTimeValue == -1)
return;
this.intervalTimeValue += this.carouselTimer.Interval;
if (this.intervalTimeValue >= this.intervalTime)
{
this.intervalTimeValue = -1;
SizePoint _SizePoint = new SizePoint();
_SizePoint.isswitch = false;
_SizePoint._original_leftleft_x = leftleftPoint.X;
_SizePoint._original_leftleft_y = leftleftPoint.Y;
_SizePoint._transparent_leftleft_x = (rightrightPoint.X - leftleftPoint.X);
_SizePoint._transparent_leftleft_y = (rightrightPoint.Y - leftleftPoint.Y);
_SizePoint._original_left_width = leftSize.Width;
_SizePoint._original_left_height = leftSize.Height;
_SizePoint._transparent_left_width = (leftleftSize.Width - leftSize.Width);
_SizePoint._transparent_left_height = (leftleftSize.Height - leftSize.Height);
_SizePoint._original_left_x = leftPoint.X;
_SizePoint._original_left_y = leftPoint.Y;
_SizePoint._transparent_left_x = (leftleftPoint.X - leftPoint.X);
_SizePoint._transparent_left_y = (leftleftPoint.Y - leftPoint.Y);
_SizePoint._original_width = centerSize.Width;
_SizePoint._original_height = centerSize.Height;
_SizePoint._transparent_width = (leftSize.Width - centerSize.Width);
_SizePoint._transparent_height = (leftSize.Height - centerSize.Height);
_SizePoint._original_x = centerPoint.X;
_SizePoint._original_y = centerPoint.Y;
_SizePoint._transparent_x = (leftPoint.X - centerPoint.X);
_SizePoint._transparent_y = (leftPoint.Y - centerPoint.Y);
_SizePoint._original_right_width = rightSize.Width;
_SizePoint._original_right_height = rightSize.Height;
_SizePoint._transparent_right_width = (centerSize.Width - rightSize.Width);
_SizePoint._transparent_right_height = (centerSize.Height - rightSize.Height);
_SizePoint._original_right_x = rightPoint.X;
_SizePoint._original_right_y = rightPoint.Y;
_SizePoint._transparent_right_x = (centerPoint.X - rightPoint.X);
_SizePoint._transparent_right_y = (centerPoint.Y - rightPoint.Y);
_SizePoint._original_rightright_width = rightrightSize.Width;
_SizePoint._original_rightright_heigh = rightrightSize.Height;
_SizePoint._transparent_rightright_width = (rightSize.Width - rightrightSize.Width);
_SizePoint._transparent_rightright_heigh = (rightSize.Height - rightrightSize.Height);
_SizePoint._original_rightright_x = rightrightPoint.X;
_SizePoint._original_rightright_y = rightrightPoint.Y;
_SizePoint._transparent_rightright_x = (rightPoint.X - rightrightPoint.X);
_SizePoint._transparent_rightright_y = (rightPoint.Y - rightrightPoint.Y);
this._Animation.AT = AnimationType.EaseOut;
this._Animation.Options.Data = _SizePoint;
this._Animation.Options.AllTime = this.animationTime;
this._Animation.Start(true, 0);
}
}
protected void Animation_AnimationIng(object sender, AnimationEventArgs e)
{
SizePoint _SizePoint = (SizePoint)e.Data;
this.carouselPictureBox[0].Location = new Point((int)(_SizePoint._original_leftleft_x + _SizePoint._transparent_leftleft_x * e.progressTime), (int)(_SizePoint._original_leftleft_y + _SizePoint._transparent_leftleft_y * e.progressTime));
if (!_SizePoint.isswitch)
{
if (this.carouselPictureBox[0].Location.X > _SizePoint._original_x)
{
_SizePoint.isswitch = true;
this.carouselPictureBoxIndex[0] = this.carouselPictureBoxIndex[4] + 1 >= this.transformImages.Count ? 0 : this.carouselPictureBoxIndex[4] + 1;
this.carouselPictureBox[0].Image = this.transformImages[this.carouselPictureBoxIndex[0]];
}
}
this.carouselPictureBox[1].Size = new Size((int)(_SizePoint._original_left_width + _SizePoint._transparent_left_width * e.progressTime), (int)(_SizePoint._original_left_height + _SizePoint._transparent_left_height * e.progressTime));
this.carouselPictureBox[1].Location = new Point((int)(_SizePoint._original_left_x + _SizePoint._transparent_left_x * e.progressTime), (int)(_SizePoint._original_left_y + _SizePoint._transparent_left_y * e.progressTime));
this.carouselPictureBox[2].Size = new Size((int)(_SizePoint._original_width + _SizePoint._transparent_width * e.progressTime), (int)(_SizePoint._original_height + _SizePoint._transparent_height * e.progressTime));
this.carouselPictureBox[2].Location = new Point((int)(_SizePoint._original_x + _SizePoint._transparent_x * e.progressTime), (int)(_SizePoint._original_y + _SizePoint._transparent_y * e.progressTime));
this.carouselPictureBox[3].Size = new Size((int)(_SizePoint._original_right_width + _SizePoint._transparent_right_width * e.progressTime), (int)(_SizePoint._original_right_height + _SizePoint._transparent_right_height * e.progressTime));
this.carouselPictureBox[3].Location = new Point((int)(_SizePoint._original_right_x + _SizePoint._transparent_right_x * e.progressTime), (int)(_SizePoint._original_right_y + _SizePoint._transparent_right_y * e.progressTime));
this.carouselPictureBox[4].Size = new Size((int)(_SizePoint._original_rightright_width + _SizePoint._transparent_rightright_width * e.progressTime), (int)(_SizePoint._original_rightright_heigh + _SizePoint._transparent_rightright_heigh * e.progressTime));
this.carouselPictureBox[4].Location = new Point((int)(_SizePoint._original_rightright_x + _SizePoint._transparent_rightright_x * e.progressTime), (int)(_SizePoint._original_rightright_y + _SizePoint._transparent_rightright_y * e.progressTime));
}
protected void Animation_AnimationEnding(object sender, AnimationEventArgs e)
{
this.locationCorrect(carouselPictureBox);
this.carouselPictureBox[0].BringToFront();
this.carouselPictureBox[4].BringToFront();
this.carouselPictureBox[1].BringToFront();
this.carouselPictureBox[3].BringToFront();
this.carouselPictureBox[2].BringToFront();
this.indexCorrect(this.carouselPictureBoxIndex);
this.intervalTimeValue = 0;
}
///
/// 初始化图片
///
public void Load()
{
if (this.carouselImages.Count > 0)
{
this.carouselPictureBoxIndex = new List<int>() { 0, 1, 2, 3, 4 };
this.resizePictureBox();
this.loadImageToPictureBox();
this.carouselPictureBox[0].BringToFront();
this.carouselPictureBox[4].BringToFront();
this.carouselPictureBox[1].BringToFront();
this.carouselPictureBox[3].BringToFront();
this.carouselPictureBox[2].BringToFront();
}
}
///
/// 开始轮播图片
///
public void Play()
{
if (!this.carouselTimer.Enabled)
{
this.carouselTimer.Start();
}
}
///
/// 停止轮播图片
///
public void Stop()
{
if (this.carouselTimer.Enabled)
{
this.carouselTimer.Stop();
}
}
///
/// 刷新轮播图片
///
public void Reset()
{
if (this.carouselTimer.Enabled)
{
this.carouselTimer.Stop();
this.Load();
this.Play();
}
}
private Size centerSize;
private Point centerPoint;
private Size leftleftSize;
private Point leftleftPoint;
private Size leftSize;
private Point leftPoint;
private Size rightSize;
private Point rightPoint;
private Size rightrightSize;
private Point rightrightPoint;
///
/// 重置PictureBox大小位置
///
private void resizePictureBox()
{
centerSize = new Size(this.Size.Width / 2, this.isReflection ? this.Size.Height / 3 * 2 : this.Size.Height / 2);
centerPoint = new Point(centerSize.Width / 2, this.isReflection ? centerSize.Height / 3 : centerSize.Height / 2);
leftSize = new Size(centerSize.Width / 2, centerSize.Height / 2);
leftPoint = new Point(centerPoint.X - leftSize.Width / 2, centerPoint.Y + leftSize.Height / 2);
leftleftSize = new Size(leftSize.Width / 2, leftSize.Height / 2);
leftleftPoint = new Point(leftPoint.X - leftleftSize.Width / 2, leftPoint.Y + leftleftSize.Height / 2);
rightSize = new Size(centerSize.Width / 2, centerSize.Height / 2);
rightPoint = new Point(centerPoint.X + centerSize.Width - rightSize.Width / 2, centerPoint.Y + rightSize.Height / 2);
rightrightSize = new Size(rightSize.Width / 2, rightSize.Height / 2);
rightrightPoint = new Point(rightPoint.X + rightSize.Width - rightrightSize.Width / 2, rightPoint.Y + rightrightSize.Height / 2);
this.carouselPictureBox[0].Size = leftleftSize;
this.carouselPictureBox[0].Location = leftleftPoint;
this.carouselPictureBox[1].Size = leftSize;
this.carouselPictureBox[1].Location = leftPoint;
this.carouselPictureBox[2].Size = centerSize;
this.carouselPictureBox[2].Location = centerPoint;
this.carouselPictureBox[3].Size = rightSize;
this.carouselPictureBox[3].Location = rightPoint;
this.carouselPictureBox[4].Size = rightrightSize;
this.carouselPictureBox[4].Location = rightrightPoint;
}
///
/// PictureBox加载对应图片
///
private void loadImageToPictureBox()
{
this.carouselPictureBoxIndex[0] = 0;
this.carouselPictureBoxIndex[1] = this.carouselPictureBoxIndex[0] + 1 >= this.carouselImages.Count ? 0 : this.carouselPictureBoxIndex[0] + 1;
this.carouselPictureBoxIndex[2] = this.carouselPictureBoxIndex[1] + 1 >= this.carouselImages.Count ? 0 : this.carouselPictureBoxIndex[1] + 1;
this.carouselPictureBoxIndex[3] = this.carouselPictureBoxIndex[2] + 1 >= this.carouselImages.Count ? 0 : this.carouselPictureBoxIndex[2] + 1;
this.carouselPictureBoxIndex[4] = this.carouselPictureBoxIndex[3] + 1 >= this.carouselImages.Count ? 0 : this.carouselPictureBoxIndex[3] + 1;
foreach (Image image in this.carouselImages)
{
this.transformImages.Add(this.isReflection ? this.reflectionTransform((Bitmap)image) : image);
}
this.carouselPictureBox[0].Image = this.transformImages[this.carouselPictureBoxIndex[0]];
this.carouselPictureBox[1].Image = this.transformImages[this.carouselPictureBoxIndex[1]];
this.carouselPictureBox[2].Image = this.transformImages[this.carouselPictureBoxIndex[2]];
this.carouselPictureBox[3].Image = this.transformImages[this.carouselPictureBoxIndex[3]];
this.carouselPictureBox[4].Image = this.transformImages[this.carouselPictureBoxIndex[4]];
}
///
/// 纠正PictureBox位置
///
/// 要纠正的PictureBox
private void locationCorrect(List
{
PictureBox pb = pbList[0];
for (int i = 0; i
{
pbList[i] = pbList[i + 1];
}
pbList[pbList.Count - 1] = pb;
}
///
/// 纠正PictureBox位置索引
///
/// 要纠正的PictureBox
private void indexCorrect(List<int> indexList)
{
int index = indexList[0];
for (int i = 0; i
{
indexList[i] = indexList[i + 1];
}
indexList[indexList.Count - 1] = index;
}
int reflectiOnTop= 10;//倒影边距
int reflectiOnBrightness= -50;//明亮度
int reflectiOnTransparentStart= 200;//倒影开始透明度
int reflectiOnTransparentEnd= -0;//倒影结束透明度
///
/// 倒影变换
///
///
/// 透明度(0-255)
private Bitmap reflectionTransform(Bitmap bmp)
{
Color pixel;
int reflectiOnHeight= bmp.Height / 2;//倒影高度
int transparentGradient = 0;//透明梯度
transparentGradient = (this.reflectionTransparentEnd - this.reflectionTransparentStart) / reflectionHeight;
if (transparentGradient == 0)
transparentGradient = 1;
Bitmap result = new Bitmap(bmp.Width, bmp.Height + reflectionHeight + this.reflectionTop);
for (int x = 0; x
{
for (int y = 0; y
{
result.SetPixel(x, y, bmp.GetPixel(x, y));
}
}
for (int y = 0; y
{
for (int x = 0; x
{
pixel = bmp.GetPixel(x, bmp.Height - 1 - y);
result.SetPixel(x, bmp.Height - 1 + this.reflectionTop + y, Color.FromArgb(this.verifyRGB(this.reflectionTransparentStart + y * transparentGradient), this.verifyRGB(pixel.R + this.reflectionBrightness), this.verifyRGB(pixel.G + this.reflectionBrightness), this.verifyRGB(pixel.B + this.reflectionBrightness)));
}
}
return result;
}
///
/// 检查RGB值ed有效范围
///
///
///
private int verifyRGB(int rgb)
{
if (rgb <0)
return 0;
if (rgb > 255)
return 255;
return rgb;
}
protected struct SizePoint
{
public bool isswitch;
public int _transparent_leftleft_x;
public int _transparent_leftleft_y;
public int _original_leftleft_x;
public int _original_leftleft_y;
public int _transparent_left_width;
public int _transparent_left_height;
public int _transparent_left_x;
public int _transparent_left_y;
public int _original_left_width;
public int _original_left_height;
public int _original_left_x;
public int _original_left_y;
public int _transparent_width;
public int _transparent_height;
public int _transparent_x;
public int _transparent_y;
public int _original_width;
public int _original_height;
public int _original_x;
public int _original_y;
public int _transparent_right_width;
public int _transparent_right_height;
public int _transparent_right_x;
public int _transparent_right_y;
public int _original_right_width;
public int _original_right_height;
public int _original_right_x;
public int _original_right_y;
public int _transparent_rightright_width;
public int _transparent_rightright_heigh;
public int _transparent_rightright_x;
public int _transparent_rightright_y;
public int _original_rightright_width;
public int _original_rightright_heigh;
public int _original_rightright_x;
public int _original_rightright_y;
}
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
if (this._Animation != null)
{
this._Animation.Dispose();
}
if (this.carouselTimer != null)
{
this.carouselTimer.Dispose();
}
}
base.Dispose(disposing);
}
}
源码下载:CSharp图片轮播.zip