任何人都可以帮助我以一定的时间间隔将目录中的图像加载到图片框中。 例如:我在\ Picture文件夹中有一些图像,如1.jpg,2.jpg ..等。 所以我的要求是遍历图片目录并将1.jpg加载到图片框中,然后等待5秒钟,然后将2.jpg加载到图片框中。
答案 0 :(得分:3)
string[] images = Directory.GetFiles(@"C:\Dir", "*.jpg");
foreach (string image in images)
{
pictureBox1.Image = new Bitmap(image);
Thread.Sleep(5000);
}
只需将此代码置于BackgroundWorker
doWork
事件中。
如果你想保持幻灯片放映Allays把它放在永远的while循环中
答案 1 :(得分:3)
终于明白了,希望它对其他人有所帮助:
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;
}
}
答案 2 :(得分:1)
将其加载到图片框中
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);
}
在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);
}
使用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);
}
答案 3 :(得分:0)
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;
}