Thursday, June 23, 2011

How to create a image slider in c#?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace ranjeet_image_demo
{
    public partial class ImageSlider : Form
    {
        int filemax = 0;
        int filecount = 0;


        public ImageSlider()
        {
            InitializeComponent();
        }
     
        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                filemax = 0;
                panel1.Controls.Clear();
                foreach (String a in Directory.GetFiles(dlg.SelectedPath))
                {
                    if ((a.EndsWith(".jpg")) || (a.EndsWith(".png")) || (a.EndsWith(".gif")) || (a.EndsWith(".bmp")) || (a.EndsWith(".jpeg")))
                    {
                        PictureBox pb = new PictureBox();
                        Image im = Image.FromFile(a);
                        pb.Image = im;
                        panel1.Controls.Add(pb);
                        filemax++;



                    }
                }



            }
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (filecount == filemax) { filecount = 0; }
            PictureBox p = (PictureBox)panel1.Controls[filecount];
            pictureBox1.Image = p.Image;
            filecount++;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void ImageSlider_Load(object sender, EventArgs e)
        {
           
            }
        }
    }

1 comment: