C# 以一定的间隔将目录中的图像加载到图片框中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8799993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Load images from directory into picture box at certain intervals
提问by Amarnath
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.
任何人都可以帮助我以一定的时间间隔将目录中的图像加载到图片框中。例如:我在 \Picture 文件夹中有一些图像,如 1.jpg,2.jpg..etc。所以我的要求是循环遍历图片目录并将1.jpg加载到图片框中然后等待5秒,然后将2.jpg加载到图片框中。
采纳答案by Amarnath
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;
}
}
回答by Burimi
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 BackgroundWorkerat doWorkEvent.
If you want to keep slideshow Allays put it in a forever while loop
只要把这个代码内BackgroundWorker的doWork事件。如果你想保持幻灯片放映,Allays 把它放在一个永远的 while 循环中
回答by irfan majeed
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
使用 image.location 从 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);
}
回答by Ckuscu
Image[] imagecache;
int cnt = 0;
private void Form1_Load(object sender, EventArgs e)
{
// change dir with your image folder. or if you want to add formats you can add them with *.jpeg etc.
string [] imageFiles = Directory.GetFiles(@"c:\dir", "*.png", SearchOption.AllDirectories);
// cache files in folder
imagecache = new Image[imageFiles.Length];
for (int i = 0; i < imageFiles.Length; i++)
{
imagecache[i] = Image.FromFile(imageFiles[i]);
}
timer1.Interval = 3000;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
public void timer1_Tick(object sender, EventArgs e)
{
picturebox.Image = null;
picturebox.Image = imagecache[cnt];
Application.DoEvents(); //to avoid memory leak in big files.
cnt++;
// if cnt exceeds files count, returns back to 0
cnt = cnt % imagecache.Length;
}

