C# 实现图片的切换

程序运行的最终效果图
整体页面
效果
主要功能介绍:程序自动加载指定目录下的所有图片,点击前后按钮可以实现图片的前后切换,三连击屏幕左上角位置,退出程序。
1、启动vs,选择“创建新项目”,项目模版选择“windows窗体应用”,点击下一步,在配置新项目页面中,确定项目名称如:MyPupol,位置,其他选项默认,最后点击“创建”,完成项目创建。
2、打开工具箱,在Form窗体中拖入一个PictureBox控件,拖入两个Button控件。
3、创建一个自定义控件,主要的功能是实现三连击退出程序。

  • 打开解决方案,选中项目,右键选择“添加”-“新建项”,选择“用户控件(windows窗体)”,确定名称:TripleButton.cs,然后点击添加按钮。

  • 在解决方案中可以看到新创建的TripleButton.cs,选中,右键选择“查看代码”,打开TripleButton.cs 的代码窗口。

  • 将下面的的代码全部复制到TripleButton.cs中,注意将命名空间修改成项目名称,
    namespace MyPupil

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ImageBigTest
{
    /// <summary>
    /// 三击按钮
    /// </summary>
    public partial class TripleButton : System.Windows.Forms.Button
    {
        /// <summary>
        ///用Event来修饰委托对象, 委托类型的事件,内部定义了签名相同的两个BtnEventClick(),
        ///其中私有化了委托类型的事件变量,并为它创建了两个方法add、remove,
        ///这两个方法就是对委托对象BtnEventClick的操作
        /// </summary>
        public event DGTripleButton BtnEventClick;

        /// <summary>
        /// 记录点击时间
        /// </summary>
        Timer tmr;

        /// <summary>
        /// 三击按钮构造函数
        /// </summary>
        public TripleButton()
        {
            base.Click += new EventHandler(TripleButton_Click);
            tmr = new Timer();
            tmr.Interval = 1000;
            tmr.Tick += new EventHandler(tmr_Tick);

        }

        /// <summary>
        /// 时钟事件,一秒以内必须连续点击三次
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void tmr_Tick(object sender, EventArgs e)
        {
            clickTimes = 0;
        }

        /// <summary>
        /// 单击次数
        /// </summary>
        int clickTimes = 0;

        /// <summary>
        /// 三击按钮执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void TripleButton_Click(object sender, EventArgs e)
        {
            //没有达到单击三次
            if (clickTimes < 2)
            {
                if (clickTimes == 0)
                {
                    tmr.Start();
                }
                clickTimes++;
            }
            else//三击之后
            {
                if (BtnEventClick != null)
                { //1. 执行委托对象的方法(指针)
                    BtnEventClick(DateTime.Now);
                }
                //2. 清空单击次数
                clickTimes = 0;

                tmr.Stop();
                tmr.Start();
            }
        }

    }
}
  • 粘贴完代码“DGTripleButton”会有红色的下划线错误,提示未能找到类型或命名空间,这里需要添加一个类,在解决方案中选中项目,右键“添加”-“新建项”,选择新建类,确定类名称:DGTripleButton.cs,点击“添加”,在打开的DGTripleButton.cs代码窗口中将下面的代码复制粘贴。同样注意修改命名空间。
    namespace MyPupil
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageBigTest
{
    /// <summary>
    /// 三击按钮委托类型
    /// </summary>
    /// <param name="time"></param>
    public delegate void DGTripleButton(DateTime time);
}
  • 将项目全部保存,在TripleButton.Designer.cs中,提示“TripleButton”未包含“AutoScaleMode”的定义。将这条语句删除或注释掉.
  • 在解决方案中选中项目,右键“生成”,在工具箱页面就出现了新创建的自定义控件TripleButton了,将自定义控件TripleButton拖入到Form窗体中,放置在Form窗体的左上角。在属性页签中设置size:45,45;Location:3,3;BackColor:SeaGreen
  • 下面为自定义控件TripleButton添加三连击事件
    在Form1.cs代码中添加如下代码
private void threeClick(DateTime time)
{
     Application.Exit();
}
  • 在Form1.cs[设计]中选中TripleButton控件,在属性中点开事件页签(⚡图标),在BtnEventClick属性中选中刚创建的threeClick事件。

4、自动加载指定目录下的所有图片,将下面代码添加到Form1_Load事件中
将图片添加到泛型集合中,通过下标访问集合中的图片

//获取应用程序的当前工作目录 /bin/debug
string rootPath = Directory.GetCurrentDirectory();
//获取指定目录的父目录 的完整路径 /bin
string parentPath = Directory.GetParent(rootPath).FullName; 
//项目根目录
string topPath = Directory.GetParent(parentPath).FullName;
//项目根目录下的img文件夹
topPath = topPath + "\\img";
//定义一个变量用于指向img目录下的某一图片完整路径
string finalPath;

//初始化指定目录的DirectoryInof 类实例
DirectoryInfo root = new DirectoryInfo(topPath);


//定义Image类型的泛型集合
List<Image> listimages = new List<Image>();
//循环获取img目录下的所有文件的完整路径并添加到链表中
foreach (FileInfo f in root.GetFiles())
{
    finalPath = topPath + "\\" + f.Name;
    listimages.Add(new Bitmap(finalPath));
}
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = listimages[0];

5、在上一步中,用image类型的泛型集合存储了img目录下的所有文件,这一步定义两个按钮,通过点击按钮改变泛型集合的下标完成切换图片的功能。在鼠标单击事件外定义一个变量指向泛型集合下标.
private int imageindex = 0;

  • 向后切换图片,泛型集合的下标递增。
private void button2_Click(object sender, EventArgs e)
{
    imageindex++;
    if (imageindex >= listimages.Count)
    {
        imageindex = 0;
    }
    pictureBox1.Image = listimages[imageindex];
}
  • 向先前切换图片,泛型集合的下标递减
private void button1_Click(object sender, EventArgs e)
{
    if(imageindex == 0)
    {
        imageindex = listimages.Count;
    }
    imageindex--;
    pictureBox1.Image = listimages[imageindex];
}

6、为按钮的点击事件绑定键盘键,即按键盘键触发按钮单击事件
首先需要将窗体Form的keyPreview属性设置为True
在这里插入图片描述

  • 然后窗体Form添加KeyDown事件,F键绑定向前选按钮,J建绑定向后选按钮,代码如下:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if(e.KeyCode == Keys.F)
    {
        button1_Click(null, null);
    }
    if(e.KeyCode == Keys.J)
    {
        button2_Click(null, null);
    }
}

7、到这里大体完成了程序的主要功能,在这一步中对窗体、按钮控件做些设置。

  • 程序运行时窗体设置,占满屏幕、无边框、底色(SeaGreen)
this.BackColor = System.Drawing.Color.SeaGreen;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.TopMost = true;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.ClientSize = new System.Drawing.Size(1920, 1080);
  • TripleButton属性设置,无边框,底色,鼠标移入(颜色同底色),鼠标按下(颜色同底色),最终效果时,程序运行是,鼠标移入、点击外观都没有变化。
this.tripleButton1.BackColor = System.Drawing.Color.SeaGreen;
this.tripleButton1.FlatAppearance.BorderSize = 0;
this.tripleButton1.FlatAppearance.MouseDownBackColor = System.Drawing.Color.SeaGreen;
this.tripleButton1.FlatAppearance.MouseOverBackColor = System.Drawing.Color.SeaGreen;
this.tripleButton1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  • Button1属性设置
this.button1.BackColor = System.Drawing.Color.SeaGreen;
this.button1.FlatAppearance.BorderSize = 0;
this.button1.FlatAppearance.MouseOverBackColor = System.Drawing.Color.SeaGreen;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Font = new System.Drawing.Font("宋体", 26.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button1.Location = new System.Drawing.Point(10, 510);
this.button1.Size = new System.Drawing.Size(25, 50);
  • Button2属性设置
this.button2.BackColor = System.Drawing.Color.SeaGreen;
this.button2.FlatAppearance.BorderSize = 0;
this.button2.FlatAppearance.MouseOverBackColor = System.Drawing.Color.SeaGreen;
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button2.Font = new System.Drawing.Font("宋体", 26.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button1.Location = new System.Drawing.Point(1885, 510);
this.button1.Size = new System.Drawing.Size(25, 50)
  • pictureBox控件扩大,在窗口加载时(Form1_Load)进行设置
wfWidth = this.Width;
wfHieght = this.Height;
pictureBox1.Width = wfWidth - 95;
pictureBox1.Height = wfHieght - 80;

最后附上源码
https://download.csdn.net/download/qq_34598215/12656957

  • 3
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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`,以便图片能够适应控件大小进行缩放。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值