以一定的间隔将目录中的图像加载到图片框中

发布于 2024-12-26 10:43:41 字数 138 浏览 0 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

じее 2025-01-02 10:43:41
string[] images = Directory.GetFiles(@"C:\Dir", "*.jpg");
foreach (string image in images)
{
  pictureBox1.Image = new Bitmap(image);
  Thread.Sleep(5000);
}

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

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

红ご颜醉 2025-01-02 10:43:41

终于明白了,希望对其他人有帮助:

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

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;
                }
            }
御守 2025-01-02 10:43:41

将其加载到图片框中,

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

在数据库中插入该路径,

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 再次从数据库中显示在图片框中

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

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);
         }
陈甜 2025-01-02 10:43:41
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;


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


    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文