Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);

       }
Posted
Updated 16-Jul-13 3:33am
v7
Comments
[no name] 13-Jul-13 15:41pm    
Can you rephrase whatever your question is? I have no idea what your question actually is or what the problem is. First thing that I see is that your delete button event handler is empty.
footballpardeep 16-Jul-13 5:40am    
May be , before deleting the image, remove it from panel.
Dhruv Shah007 16-Jul-13 5:43am    
sir its a good idea ..can u tell me how?

Hi Dhruv,

From the code you showed in your question I assume you are experimenting with kind of prototype. If you want to go that way better implement a custom control or at least a composed user control (out of Label and picturebox). Of course then you need a kind of "list" to present/select the individual pictures.
I would implement a custom control (OwnerDrawn) and get rid of useless PictureBox/Label controls by just painting the Image/Text in a custom control's paint handler (this should boost perfomance) and you can easier control things like DoubleBuffering.


An alternative could be just using a prepared control doing exactly what you showed in your code - a ListView with the property View set to View.Tile

Do you have any specific problems with your current approach?

Btw. for this kind of app I would highly recommend using WPF and not Windows.Forms as presentation technology. (But that's another story)

just my 2 cents (or what currency you use ;-)

kind regards Johannes
 
Share this answer
 
Comments
Dhruv Shah007 16-Jul-13 9:16am    
sir whatever you just said is right but i have some other issue and one more issue is o=in arranging the picture boxes only four in a row.
After arranging the picture boxes when I am dragging the panel and again calling the button1_Click event the place of images getting destructed from them place.
Try this,

PictureBox picToDelete;


modify this function.

C#
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 picToDelete = (PictureBox)(sender);
          

       }


C#
private void btnDELETE_Click(object sender, EventArgs e)
       { 
 panel1.Controls.Remove(picToDelete);


           File.Delete(ImageToShow);
           //button1.PerformClick();
           MessageBox.Show(&quot;The image is deleted successfully...!!!&quot;, &quot;Information&quot;,  MessageBoxButtons.OK, MessageBoxIcon.Information);

       }</pre>
 
Share this answer
 
Comments
Dhruv Shah007 17-Jul-13 4:52am    
I tried this ..but its not working sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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