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:
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.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];
for (int i = 0; i < Count; i++)
{
pb[i] = new System.Windows.Forms.PictureBox();
}
}
private void ClickImage(Object sender, System.EventArgs e)
{
ImageToShow = ((System.Windows.Forms.PictureBox)sender).Tag.ToString();
}
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;
for (int i = 0; i < photo_Count; ++i)
{
Label name = new Label();
im = GetCopyImage(imgName[i]);
pb[i].Image = im;
if (xpos > 920)
{
xpos = 5;
ypos = ypos + 240;
}
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);
MessageBox.Show("The image is deleted successfully...!!!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}