Image Gallery in your windows application in C#

 
This article demonstrate how we can create a simple image gallery in C#, this article is the c# version of the original article in vb.net(). In this article we create a usercontrol for doing it, our control will be create thumbnails of the images of the specified directory.
means you just need to give the your directory path.

Image Gallery in vb.net
Image Gallery in vb.net

For creating your Image Gallery usercontrol, follow these steps:
1. Add a usercontrol in your project and give the name AuthorCodeImageGalleryVB.
2. Put the following code in your usercontrol class.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
 
namespace ImageGallery_In_cSharp
{
    public partial class AuthorCodeImageGallerycSharp : UserControl
    {
        public AuthorCodeImageGallerycSharp()
        {
            InitializeComponent();
        }
        int CtrlWidth;
        int CtrlHeight;
        int PicWidth;
        int PicHeight;
        int XLocation;
        int YLocation;
        private void AuthorCodeImageGalleryVB_Resize(System.Object sender, System.EventArgs e)
        {
            CtrlHeight = this.Height;
            CtrlWidth = this.Width;
        }
        private string _Directory_Path;
        public string Directorypath
        {
            get { return _Directory_Path; }
            set
            {
                _Directory_Path = value;
                XLocation = 25;
                YLocation = 25;
                PicWidth = 117;
                PicHeight = 109;
                CreateGallery();
            }
        }
        int i = 0;
        private void DrawPictureBox(string _filename, string _displayname)
        {
            PictureBox Pic1 = new PictureBox();
            Pic1.Location = new System.Drawing.Point(XLocation, YLocation);
            XLocation = XLocation + PicWidth + 20;
            if (XLocation + PicWidth >= CtrlWidth)
            {
                XLocation = 25;
                YLocation = YLocation + PicHeight + 20;
            }
            Pic1.Name = "PictureBox" + i;
            i += 1;
            Pic1.Size = new System.Drawing.Size(PicWidth, PicHeight);
            Pic1.TabIndex = 0;
            Pic1.TabStop = false;
            Pic1.BorderStyle = BorderStyle.Fixed3D;
            this.toolTip1.SetToolTip(Pic1, _displayname);
            Pic1.MouseEnter += Pic1_MouseEnter;
            Pic1.MouseLeave += Pic1_MouseLeave;
            this.Controls.Add(Pic1);
            Pic1.Image = Image.FromFile(_filename);
            Pic1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
        }
        private void CreateGallery()
        {
            i = 0;
            RemoveControls();
            if (Directorypath != null)
            {
                System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Directorypath);
 
                System.IO.FileInfo[] diar1 = di.GetFiles("*.jpg");
                System.IO.FileInfo[] diar2 = di.GetFiles("*.bmp");
                System.IO.FileInfo[] diar3 = di.GetFiles("*.png");
                System.IO.FileInfo[] diar4 = di.GetFiles("*.gif");
 
                var diarList = new List<System.IO.FileInfo>();
                diarList.AddRange(diar1);
                diarList.AddRange(diar2);
                diarList.AddRange(diar3);
                diarList.AddRange(diar4);
                System.IO.FileInfo[] diar = diarList.ToArray();
 
                System.IO.FileInfo dra = null;
                foreach (System.IO.FileInfo dra_loopVariable in diar1)
                {
                    dra = dra_loopVariable;
                    DrawPictureBox(dra.FullName, dra.Name);
                }
            }
        }
 
        private void Pic1_MouseEnter(System.Object sender, System.EventArgs e)
        {
            PictureBox Pic = default(PictureBox);
            Pic = (PictureBox)sender;
            Pic.BorderStyle = BorderStyle.FixedSingle;
        }
        private void Pic1_MouseLeave(System.Object sender, System.EventArgs e)
        {
            PictureBox Pic = default(PictureBox);
            Pic = (PictureBox)sender;
            Pic.BorderStyle = BorderStyle.Fixed3D;
        }
        private void RemoveControls()
        {
        Again:
            foreach (Control ctrl in this.Controls)
            {
                if ((ctrl) is PictureBox)
                {
                    this.Controls.Remove(ctrl);
                }
            }
            if (this.Controls.Count > 0)
            {
                goto Again;
            }
        }
    }
}

For making this usercontrol we use the picturebox coontrol, In above control we create dynamic Picturebox controls for each image. We find all image type files with .bmp,.jpg,.gif and .png in the given direcotory with the help of GetFiles method of IO.DirectoryInfo class

How to use this control
1. Build your project. You can see this usercontrol in your toolbox.
2. Just drag and drop in your form.
3. Set its property Directorypath.
suppose you place this control on the form named Form1, then you can set its property Directorypath with the help of folderbrowserDialoog:

Imports System.IO
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace ImageGallery_In_cSharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            dlg.ShowDialog();
            authorCodeImageGallerycSharp1.Directorypath = dlg.SelectedPath;
        }
    }
}

2 thoughts on “Image Gallery in your windows application in C#”

  1. Hi,
    How can i display images from SQL Server in same fashion according to some category selected by user.

Comments are closed.