WPF C# Tutorial – Make a simple image gallery in visual studio

This is a simple project and please double check the full code below to make sure you have followed it correctly.

Full Source Code –

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace image_gallery
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        int i = 1; // this integer will start with a 1

        public MainWindow()
        {
            InitializeComponent();
        }

        private void goNext(object sender, RoutedEventArgs e)
        {
            i++; // increase i by 1
            
            // if i's value gets larger than 6 then reset i back to 1

            if (i > 6)
            {
                i = 1;
            }

            // change the picture according to the i's value
            picHolder.Source = new BitmapImage(new Uri(@"pics/" + i + ".jpg", UriKind.Relative));
        }

        private void goBack(object sender, RoutedEventArgs e)
        {
            i--; // this will decrease 1 from i


            // if the value of i is less than 1
            // then give i the value of 6
            if (i < 1)
            {
                i = 6;
            }

            // change the picture according to the i's value
            picHolder.Source = new BitmapImage(new Uri(@"pics/" + i + ".jpg", UriKind.Relative));
        }
    }
}

 




One response to “WPF C# Tutorial – Make a simple image gallery in visual studio”

  1. ComputerEngineer says:

    Hey bro thank you so much . You saved my life and my school life.