C#图片轮播

       

     

图片轮播无缝版:

  public partial class ImageCarouselExt : Control
  {

    #region
    /// <summary>
    /// 动画播放时间间隔定时器
    /// </summary>
    private Timer carouselTimer = new Timer();
    /// <summary>
    /// 轮播的三个PictureBox
    /// </summary> 
    private List<PictureBox> carouselPictureBox = new List<PictureBox>() { new PictureBox(), new PictureBox(), new PictureBox() };

    /// <summary>
    /// 轮播的三个PictureBox位置索引
    /// </summary>
    private List<int> locationPictureBox = new List<int>() { -1, 0, 1 };

    /// <summary>
    /// 图片轮播的时间间隔累计(-1为动画正在切换中)
    /// </summary>
    private int intervalTimeValue = 0;

    private int original_x = 0;// 动画对象开始制定属性原始值
    private int original_y = 0;// 动画对象开始制定属性原始值

    private AnimationTimer _Animation;

    #endregion

    #region

    private List<Image> carouselImages = new List<Image>();
    /// <summary>
    /// 要播放的图片
    /// </summary>
    [Description("要播放的图片")]
    public List<Image> CarouselImages
    {
      get { return carouselImages; }
      set
      {
        carouselImages = value;
        this.Reset();
      }
    }

    private double animationTime = 500;
    /// <summary>
    /// 动画播放的总时间
    /// </summary>
    [DefaultValue(500)]
    [Description("动画播放的总时间(默认500毫秒)")]
    public double AnimationTime
    {
      get { return animationTime; }
      set
      {
        animationTime = value;
        this.Reset();
      }
    }

    private int intervalTime = 1000;
    /// <summary>
    /// 图片轮播的时间间隔
    /// </summary>
    [DefaultValue(500)]
    [Description("图片轮播的时间间隔(默认1000毫秒)")]
    public int IntervalTime
    {
      get { return intervalTime; }
      set
      {
        intervalTime = value;
        this.Reset();
      }
    }

    #endregion

    public ImageCarouselExt()
    {
      InitializeComponent();
      this.Controls.Add(carouselPictureBox[0]);
      this.Controls.Add(carouselPictureBox[1]);
      this.Controls.Add(carouselPictureBox[2]);
      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);
    }

    /// <summary>
    /// 定时器
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    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;
        this.original_x = this.carouselPictureBox[1].Location.X;
        this.original_y = this.carouselPictureBox[1].Location.Y;

        this._Animation.AT = AnimationType.EaseOut;
        this._Animation.Options.Transform = -this.Width;
        this._Animation.Options.AllTime = this.animationTime;
        this._Animation.Start(true, 0);
      }
    }

    protected void Animation_AnimationIng(object sender, AnimationEventArgs e)
    {
      Point point = new Point((int)(this.original_x + e.Transform * e.progressTime), 0);//计算动画值
      this.carouselPictureBox[0].Location = new Point(point.X - this.Width, point.Y);
      this.carouselPictureBox[1].Location = point;
      this.carouselPictureBox[2].Location = new Point(point.X + this.Width, point.Y);
    }

    protected void Animation_AnimationEnding(object sender, AnimationEventArgs e)
    {
      this.carouselPictureBox[0].Location = new Point(this.Width, 0);//移动第一个PictureBox到第三
      this.locationCorrect(carouselPictureBox);
      this.loadImageToPictureBox();

      this.carouselPictureBox[0].Location = new Point(-this.Width, 0);
      this.carouselPictureBox[1].Location = new Point(0, 0);
      this.carouselPictureBox[2].Location = new Point(this.Width, 0);
      this.intervalTimeValue = 0;
    }

    /// <summary>
    /// 初始化图片
    /// </summary>
    public void Load()
    {
      if (this.carouselImages.Count > 0)
      {
        this.locationPictureBox = new List<int>() { -1, 0, 1 };
        this.resizePictureBox();
        this.loadImageToPictureBox();
      }
    }

    
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# 中实现图片可以使用 `Timer` 控件来定时切换图片。以下是一个简单的示例代码: ```csharp public partial class Form1 : Form { private int currentIndex = 0; // 当前显示的图片索引 private List<Image> images = new List<Image>(); // 图片列表 private Timer timer; // 定时器 public Form1() { InitializeComponent(); // 初始化图片列表 images.Add(Properties.Resources.pic1); images.Add(Properties.Resources.pic2); images.Add(Properties.Resources.pic3); // 初始化定时器 timer = new Timer(); timer.Interval = 2000; // 设置定时器间隔为 2 秒 timer.Tick += Timer_Tick; // 绑定定时器 Tick 事件处理程序 timer.Start(); // 启动定时器 } private void Timer_Tick(object sender, EventArgs e) { // 切换图片 currentIndex = (currentIndex + 1) % images.Count; pictureBox1.Image = images[currentIndex]; } } ``` 在这个示例代码中,我们使用了 `List<Image>` 来存储图片,并使用 `Timer` 控件来定时切换图片。在窗体的构造函数中,我们初始化了图片列表和定时器,并将定时器启动。定时器每隔 2 秒触发一次 `Tick` 事件,在事件处理程序中切换当前显示的图片。当图片索引达到列表末尾时,会循环回到列表头部。 上述代码中,`pictureBox1` 是一个 `PictureBox` 控件,用于显示图片。在窗体设计器中,我们需要将 `pictureBox1` 的 `SizeMode` 属性设置为 `StretchImage`,以便图片能够适应控件大小进行缩放。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值