Click here to Skip to main content
15,891,431 members

dynamically generate image gallery in C#.net form application

Dhruv Shah007 asked:

Open original thread
I am working on Image gallery .In which I have dynamically create picturebox and label for giving image name.Now after getting load all images in to pictureboxes in a panel , issue is in arranging the picture boxes, only four in a row. After arranging the picture boxes when I am drag down the panel and again calling the button1_Click event the place of images getting destructed from them place.The images are loading on different location means it's getting over-write.please help me out.
Images links from where you can get exact idea
http://postimg.org/image/5tm5gzj87/
http://postimg.org/image/4sridmuuj/

here is my code.
please help me out..

CODE:
C#
public partial class Form1 : Form
   {
       int photo_Count, a;
       PictureBox[] pb;
       private string[] imgName;
       public static string ImageToShow;
       string _directoryName = @"./Snapshot";
       Image im;
       Bitmap bm;
       // System.IO.Directory mydir;
       System.IO.DirectoryInfo dirinfo;
       public Form1()
       {
           InitializeComponent();
           panel1.Top = (this.ClientSize.Height - panel1.Height) / 2;
           panel1.Left = (this.ClientSize.Width - panel1.Width) / 2;
       }

       private void AddControls(int Count)
       {
           pb = new System.Windows.Forms.PictureBox[Count]; // assign number array

           for (int i = 0; i < Count; i++)
           {
               pb[i] = new System.Windows.Forms.PictureBox(); // Initialize one variable
           }
           // When call this function you determine number of controls
       }

       // The Event to click the image
       private void ClickImage(Object sender, System.EventArgs e)
       {


           // On Click: load (ImageToShow) with (Tag) of the image

           ImageToShow = ((System.Windows.Forms.PictureBox)sender).Tag.ToString();

           //PictureBox picBox = (PictureBox)(sender);
           //picBox.BorderStyle = BorderStyle.Fixed3D;

       }
       private Image GetCopyImage(string path)
       {
           a = 1;
           using (im = Image.FromFile(path))
           {

               bm = new Bitmap(im);
               return bm;

           }
       }

       private void button1_Click(object sender, EventArgs e)
       {


           if (Directory.Exists(_directoryName))
           {
               imgName = Directory.GetFiles(Application.StartupPath + @"\Snapshot");
               dirinfo = new System.IO.DirectoryInfo(_directoryName);
               photo_Count = dirinfo.GetFiles("*jpg").Count();

               if (photo_Count != 0)
               {


                   AddControls(photo_Count);
                   int xpos = 5;
                   int ypos = 5;

                   //      MessageBox.Show("Total No of traked images in the directory are : " + photo_Count);

                   for (int i = 0; i < photo_Count; ++i)
                   {
                       Label name = new Label();
                       //*******************Load tracked Images from the folder*********************//

                       im = GetCopyImage(imgName[i]);
                       pb[i].Image = im;

                       if (xpos > 920)
                       {
                           xpos = 5; // leave eight pixels at Left
                           ypos = ypos + 240;  // height of image + 220
                       }
                       pb[i].Left = xpos;
                       pb[i].Top = ypos;
                       pb[i].Size = new Size(300, 220);
                       pb[i].BorderStyle = BorderStyle.None;

                       pb[i].SizeMode = PictureBoxSizeMode.Zoom;
                       pb[i].BackgroundImageLayout = ImageLayout.Tile;
                       pb[i].Cursor = System.Windows.Forms.Cursors.Hand;
                       panel1.Controls.Add(pb[i]);
                       pb[i].BringToFront();
                       pb[i].Tag = imgName[i];
                       pb[i].Click += new System.EventHandler(ClickImage);

                       var filenNme = Path.GetFileName(imgName[i]);
                       name.Text = filenNme.ToString();
                       name.Size = new Size(300, 30);
                       name.Left = xpos;
                       name.Top = ypos + 220;
                       name.Tag = imgName[i];
                       panel1.Controls.Add(name);
                       xpos = xpos + 305;

                   }
               }
               else
               {
                   MessageBox.Show("There is no tracked image in directory...!!!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
               }
           }
           else
           {
               if (MessageBox.Show("There is no directory for Tracked Images...!!!", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) ;
               {
                   System.IO.Directory.CreateDirectory(_directoryName);

               }

           }
       }

       private void btnDELETE_Click(object sender, EventArgs e)
       {
           File.Delete(ImageToShow);
           //button1.PerformClick();
           MessageBox.Show("The image is deleted successfully...!!!", "Information",  MessageBoxButtons.OK, MessageBoxIcon.Information);

       }
Tags: C# (C# 4.0)

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900