C# Images Slider From MySQL Database

How To Make Images SlideShow From MySQL Database Using C#

C# Images Slider From Database


In This C# Tutorial we will See How To Make an Image SlideShow With Images From MySQL Database Table in C# And Visual Studio Editor .

we need to create two buttons:
1-  "NEXT" => go to the next image in the mysql database table
2- "PREVIOUS" => go to the previous image in the mysql database table



WATCH THIS C# TUTORIAL


Project Source Code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO;

namespace Csharp_Tutorials
{
    public partial class Images_SlideShow_From_MySQL : Form
    {
        public Images_SlideShow_From_MySQL()
        {
            InitializeComponent();
        }

// the mysql connection
        MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='student_db';username=root;password=");

        // create a datatable
        DataTable table = new DataTable();

        // int variable to get the row index
        int rowIndex;

        // the form load
        private void Images_SlideShow_From_MySQL_Load(object sender, EventArgs e)
        {

            MySqlCommand command = new MySqlCommand("SELECT picture FROM `student`", connection);

            MySqlDataAdapter adapter = new MySqlDataAdapter(command);

            adapter.Fill(table);

            rowIndex = 0;

            showImage();

        }


        // create a method to display image depending on the rowindex value
        public void showImage()
        {
            Byte[] imgByte = (Byte[])table.Rows[rowIndex][0];

            MemoryStream ms = new MemoryStream(imgByte);

            pictureBox1.Image = Image.FromStream(ms);

        }


        // button next image
        private void button_Next_Click(object sender, EventArgs e)
        {
            if(rowIndex < table.Rows.Count -1)
            {
                rowIndex++;
                showImage();
            }
            
        }

      
       // button previous
        private void button_Previous_Click(object sender, EventArgs e)
        {
            if (rowIndex >= 1)
            {
                rowIndex--;
                showImage();
            }
            
        }
    }
}



OUTPUT:


images slider in c#




Share this

Related Posts

Previous
Next Post »