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.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace ImageGallery_In_cSharp
  10. {
  11.     public partial class AuthorCodeImageGallerycSharp : UserControl
  12.     {
  13.         public AuthorCodeImageGallerycSharp()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         int CtrlWidth;
  18.         int CtrlHeight;
  19.         int PicWidth;
  20.         int PicHeight;
  21.         int XLocation;
  22.         int YLocation;
  23.         private void AuthorCodeImageGalleryVB_Resize(System.Object sender, System.EventArgs e)
  24.         {
  25.             CtrlHeight = this.Height;
  26.             CtrlWidth = this.Width;
  27.         }
  28.         private string _Directory_Path;
  29.         public string Directorypath
  30.         {
  31.             get { return _Directory_Path; }
  32.             set
  33.             {
  34.                 _Directory_Path = value;
  35.                 XLocation = 25;
  36.                 YLocation = 25;
  37.                 PicWidth = 117;
  38.                 PicHeight = 109;
  39.                 CreateGallery();
  40.             }
  41.         }
  42.         int i = 0;
  43.         private void DrawPictureBox(string _filename, string _displayname)
  44.         {
  45.             PictureBox Pic1 = new PictureBox();
  46.             Pic1.Location = new System.Drawing.Point(XLocation, YLocation);
  47.             XLocation = XLocation + PicWidth + 20;
  48.             if (XLocation + PicWidth >= CtrlWidth)
  49.             {
  50.                 XLocation = 25;
  51.                 YLocation = YLocation + PicHeight + 20;
  52.             }
  53.             Pic1.Name = "PictureBox" + i;
  54.             i += 1;
  55.             Pic1.Size = new System.Drawing.Size(PicWidth, PicHeight);
  56.             Pic1.TabIndex = 0;
  57.             Pic1.TabStop = false;
  58.             Pic1.BorderStyle = BorderStyle.Fixed3D;
  59.             this.toolTip1.SetToolTip(Pic1, _displayname);
  60.             Pic1.MouseEnter += Pic1_MouseEnter;
  61.             Pic1.MouseLeave += Pic1_MouseLeave;
  62.             this.Controls.Add(Pic1);
  63.             Pic1.Image = Image.FromFile(_filename);
  64.             Pic1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
  65.         }
  66.         private void CreateGallery()
  67.         {
  68.             i = 0;
  69.             RemoveControls();
  70.             if (Directorypath != null)
  71.             {
  72.                 System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Directorypath);
  73.  
  74.                 System.IO.FileInfo[] diar1 = di.GetFiles("*.jpg");
  75.                 System.IO.FileInfo[] diar2 = di.GetFiles("*.bmp");
  76.                 System.IO.FileInfo[] diar3 = di.GetFiles("*.png");
  77.                 System.IO.FileInfo[] diar4 = di.GetFiles("*.gif");
  78.  
  79.                 var diarList = new List<System.IO.FileInfo>();
  80.                 diarList.AddRange(diar1);
  81.                 diarList.AddRange(diar2);
  82.                 diarList.AddRange(diar3);
  83.                 diarList.AddRange(diar4);
  84.                 System.IO.FileInfo[] diar = diarList.ToArray();
  85.  
  86.                 System.IO.FileInfo dra = null;
  87.                 foreach (System.IO.FileInfo dra_loopVariable in diar1)
  88.                 {
  89.                     dra = dra_loopVariable;
  90.                     DrawPictureBox(dra.FullName, dra.Name);
  91.                 }
  92.             }
  93.         }
  94.  
  95.         private void Pic1_MouseEnter(System.Object sender, System.EventArgs e)
  96.         {
  97.             PictureBox Pic = default(PictureBox);
  98.             Pic = (PictureBox)sender;
  99.             Pic.BorderStyle = BorderStyle.FixedSingle;
  100.         }
  101.         private void Pic1_MouseLeave(System.Object sender, System.EventArgs e)
  102.         {
  103.             PictureBox Pic = default(PictureBox);
  104.             Pic = (PictureBox)sender;
  105.             Pic.BorderStyle = BorderStyle.Fixed3D;
  106.         }
  107.         private void RemoveControls()
  108.         {
  109.         Again:
  110.             foreach (Control ctrl in this.Controls)
  111.             {
  112.                 if ((ctrl) is PictureBox)
  113.                 {
  114.                     this.Controls.Remove(ctrl);
  115.                 }
  116.             }
  117.             if (this.Controls.Count > 0)
  118.             {
  119.                 goto Again;
  120.             }
  121.         }
  122.     }
  123. }

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:

  1. Imports System.IO
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace ImageGallery_In_cSharp
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private void button1_Click(object sender, EventArgs e)
  20.         {
  21.             FolderBrowserDialog dlg = new FolderBrowserDialog();
  22.             dlg.ShowDialog();
  23.             authorCodeImageGallerycSharp1.Directorypath = dlg.SelectedPath;
  24.         }
  25.     }
  26. }

facebooktwittergoogle_plusredditpinterestlinkedinmail
  • Pepe

    The name ‘InitializeComponent’ does not exist in the current context

  • Zahid

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