Tạo ứng dụng Slide Show ảnh trong C#


Trong bài thực hành ngày, tôi sẽ giới thiệu cách lập trình một ứng dụng Slide show ảnh sử dụng ngôn ngữ C#

  1. Mở chương trình Visual Studio 2010: Start >> All Programs >> Microsft Visual Studio 2010 >> Visual Studio 2010
  2. Chọn menu “File” -> “New” -> “Project…
  3. Chọn ngôn ngữ “Visual C#” và loại ứng dụng mà mình bạn muốn tạo là “Windows Forms Application”, đặt tên ứng dụng của bạn ở ô “Name” là: “SlideShow“. Kích vào nút “OK” để hoành thành
  4. Trên thanh Toolbox và tìm control có tên là Panel rồi kéo lên Form và thiết lập thuộc tính Dock = Right, panel này có tên mặc định là panel1.
  5. Đặt một control có tên là PictureBox vào trong panel1 thiết lập thuộc tính Dock = Fill.
  6. Đây là giao diện thiết kế trên Visual Studio 2010:

Thiết kế SlideShow ảnh

  1. Kéo thả control có tên FolderBrowserDiaglogTimer lên Form. Nó sẽ xuất hiện ở phía dưới. Chọn vào control Timer và thiết lập thuộc tính Interval=3000.
  2. Trên các nút (button) next(>>), previous(<<) và Slide Show thiết lập thuộc tính Enable = False, mục đích chúng ta sẽ thiết lập mặc định các nút này bị mờ đi.
  3. Import thư viện “System.IO” bằng câu lệnh using System.IO.
  4. Kích chuột vào Form, chọn View Code từ menu đổ ra. Các bạn viết code như sau:

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;
using System.IO;


namespace SlideShow
{
    public partial class frmSlideShow : Form
    {
        string Dirpath;
        int imgindex;

        public frmSlideShow()
        {
            InitializeComponent();
        }

        private void frmSlideShow_Load(object sender, EventArgs e)
        {
            for (int i = 1; i < 10; i++)
            {
                comboBox1.Items.Add(i);
                comboBox1.SelectedIndex = 0;
            }
        }

        private void btnLoadImage_Click(object sender, EventArgs e)
        {
            DialogResult dr = folderBrowserDialog1.ShowDialog();
            if (dr != DialogResult.Cancel)
            {
                listBox1.Items.Clear();
                Dirpath = folderBrowserDialog1.SelectedPath;
                string[] files = Directory.GetFiles(Dirpath, "*.png");
                foreach (string file in files)
                {
                    int pos = file.LastIndexOf("||");
                    string FName = file.Substring(pos + 1);
                    listBox1.Items.Add(FName);
                }
                listBox1.SelectedIndex = imgindex = 0;
                btnPrev.Enabled = true;
                btnNext.Enabled = btnSlideShow.Enabled = true;
            }

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            pictureBox1.ImageLocation = listBox1.SelectedItem.ToString();
            // pictureBox1.ImageLocation = Dirpath + "\\" + listBox1.SelectedItem;
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            if (imgindex > 0)
            {
                imgindex -= 1;
                if (imgindex == 0)
                {
                    btnPrev.Enabled = false;
                }
                if (imgindex < listBox1.Items.Count - 1)
                    btnNext.Enabled = true;
                listBox1.SelectedIndex = imgindex;
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (imgindex < listBox1.Items.Count - 1)
            {
                imgindex += 1;
                if (imgindex == listBox1.Items.Count - 1)
                    btnNext.Enabled = false;
                if (imgindex > 0)
                    btnPrev.Enabled = true;
                listBox1.SelectedIndex = imgindex;
            }

        }

        private void btnSlideShow_Click(object sender, EventArgs e)
        {
            if (btnSlideShow.Text == "Slide Show")
            {
                btnSlideShow.Text = "Stop Show";
                timer1.Interval = int.Parse(comboBox1.Text) * 1000;
                timer1.Start();
            }
            else
            {
                timer1.Stop();
                btnSlideShow.Text = "Slide Show";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            btnNext.PerformClick();
        }
    }
}

  1. Bây giờ bạn lưu ứng dụng rồi chạy ứng dụng. Kích vào nút “Load Images”, tìm tới thư mục chứa ảnh. Rồi click “OK”. Kích các nút để di chuyển các ảnh trước hoặc sau. Kích trên nút “Start Show” để hiển thị theo kiểu chạy slide. Kích “Stop Show” để dừng việc hiển thị ảnh theo slide.
  2. Bạn có thể download Code SlideShow ở đây.

Có thể bạn sẽ thích…

Trả lời

EnglishVietnamese