【问题标题】:Load images from directory into picture box at certain intervals以一定的时间间隔将目录中的图像加载到图片框中
【发布时间】:2012-01-10 07:41:30
【问题描述】:

任何人都可以帮助我以一定的时间间隔将图像从目录加载到图片框中。 例如:我在 \Picture 文件夹中有一些图像,如 1.jpg、2.jpg..等。 所以我的要求是遍历图片目录并将1.jpg加载到图片框然后等待5秒,然后将2.jpg加载到图片框。

【问题讨论】:

  • 嗨 Cody 我写了类似 foreach(string fileName in Directory.GetFiles"C:\\Users\\Desktop\\Image")) { pictureBox1.Image = Image.FromFile(fileName);但我在一定间隔加载其他图像时感到震惊。
  • 您尝试过使用计时器吗?您可以将其设置为您想要的任何间隔。 System.Windows.Forms.Timer 是您想要的类的名称,或者您可以从工具箱中将其拖到表单中。
  • 只需转到 google 并输入“slideshow c# winforms”。你会得到无数的结果。甚至你可能会得到堆栈溢出结果。
  • 感谢您的快速回复。是的,我使用过计时器,例如:System.Timers.Timer timer = new System.Timers.Timer(5000);在这 5 秒之后,我如何将第二张图片从 Images 目录推送到图片框。
  • 哎呀!注意我说要使用System.Windows.Forms.Timer!

标签: c# .net winforms picturebox


【解决方案1】:
string[] images = Directory.GetFiles(@"C:\Dir", "*.jpg");
foreach (string image in images)
{
  pictureBox1.Image = new Bitmap(image);
  Thread.Sleep(5000);
}

只需将此代码放入 BackgroundWorker doWork 事件中即可。 如果你想保留幻灯片 Allays 把它放在一个永远的 while 循环中

【讨论】:

  • 没有这给出了循环图像的想法,但我使用 Timer_Tick 事件处理代码,如图所示:pictureBox1.Left = rand.Next(Math.Max(1, Bounds.Width - pictureBox1.Width)) ; pictureBox1.Top = rand.Next(Math.Max(1, Bounds.Height - pictureBox1.Height));
  • 图片 image = Image.FromFile(images[_counter]);图片框1.宽度=图像.宽度;图片框1.高度=图像.高度;图片框1.图像=图像; if (_counter
【解决方案2】:

终于搞定了,希望对大家有帮助:

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;
                }
            }

【讨论】:

    【解决方案3】:

    加载到图片框中

    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);
             }
    

    【讨论】:

      【解决方案4】:
      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;
      
      
          }
      

      【讨论】:

      • 请添加一些描述。
      • @OmSao 根据要求添加了一些 cmets。
      • Application.DoEvents();以避免大文件夹中的内存泄漏。不是文件:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 2011-10-23
      • 1970-01-01
      相关资源
      最近更新 更多