Load images from directory into picture box at certain intervals
28,215
Solution 1
Finally got it, hope it will be helpful for others:
private void Form_Load(object sender, EventArgs e)
{
moveTimer.Interval = 1000;
moveTimer.Tick += new EventHandler(moveTimer_Tick);
moveTimer.Start();
}
private void moveTimer_Tick(object sender, System.EventArgs e)
{
string[] images = Directory.GetFiles(@"C:\Dir", "*.jpg");
image = Image.FromFile(images[counter]);
pictureBox.Width = image.Width;
pictureBox.Height = image.Height;
pictureBox.Image = image;
// Move Image to new location
pictureBox.Left = rand.Next(Math.Max(0, Bounds.Width - pictureBox.Width));
pictureBox.Top = rand.Next(Math.Max(0, Bounds.Height - pictureBox.Height));
if (counter < images.Count - 1)
{
counter = counter + 1;
}
else
{
counter = 0;
}
}
Solution 2
string[] images = Directory.GetFiles(@"C:\Dir", "*.jpg");
foreach (string image in images)
{
pictureBox1.Image = new Bitmap(image);
Thread.Sleep(5000);
}
Just put this Code inside a BackgroundWorker
at doWork
Event.
If you want to keep slideshow Allays put it in a forever while loop
Solution 3
Load it in picture box
var _with1 = openFileDialog1;
_with1.Filter = ("Image Files |*.png; *.bmp; *.jpg;*.jpeg; *.gif;");
_with1.FilterIndex = 4;
//Reset the file name
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox2.Image = Image.FromFile(openFileDialog1.FileName);
}
insert that path in db
try
{
con = new OleDbConnection(cs);
con.Open();
cmd = new OleDbCommand(cs);
string cb = "insert into colorcodes(color,pic) VALUES ('" + colorcb.Text + "','" + openFileDialog1.FileName + "' )";
cmd = new OleDbCommand(cb);
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("image Saved Successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
use image.location to show in picture box again from db
try
{
con = new OleDbConnection(cs);
con.Open();
cmd = new OleDbCommand("SELECT pic from colorcodes where color= '" + colorcb.Text + "' ", con);
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
dr.Read();
pictureBox2.ImageLocation = dr[0].ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Author by
Amarnath
Updated on September 28, 2020Comments
-
Amarnath over 3 years
Can anyone help me in loading images from directory into picture box at certain intervals. For eg: i have some images in \Picture folder like 1.jpg,2.jpg..etc. So my requirement is to loop through Picture Directory and load 1.jpg into Picture box then wait for 5 sec, and then load 2.jpg into picture box.
-
Amarnath about 12 yearsNo this has given idea to loop through images but i handled code using Timer_Tick event as shown:pictureBox1.Left = rand.Next(Math.Max(1, Bounds.Width - pictureBox1.Width)); pictureBox1.Top = rand.Next(Math.Max(1, Bounds.Height - pictureBox1.Height));
-
Amarnath about 12 yearsImage image = Image.FromFile(images[_counter]); pictureBox1.Width = image.Width; pictureBox1.Height = image.Height; pictureBox1.Image = image; if (_counter < images.Count -1) { _counter = _counter + 1; } else { _counter = 0; }
-
Om Sao over 6 yearsPlease add some description too.
-
Ckuscu over 6 years@OmSao added some comments as requested.
-
Ckuscu over 6 yearsApplication.DoEvents(); to avoid memory leak in big folders. not files :)