2012-01-10 15 views
2

ディレクトリから画像ボックスへ画像を一定の間隔で読み込むのに誰も助けてくれますか? 例:1.jpg、2.jpgなどの\ Pictureフォルダにいくつかの画像があります。 私の要件はピクチャディレクトリをループし、ピクチャボックスに1.jpgをロードしてから5秒間待ってから2.jpgを画像ボックスにロードすることです。ディレクトリから画像ボックスに一定の間隔で画像をロードする

+0

こんにちはコーディdbが、私はforeachのようなコードを書かれているからAIN(Directory.GetFiles内の文字列のファイル名 "C:\\ユーザー\\デスクトップ\\画像")){pictureBox1.Image = Image.FromFile (ファイル名); }しかし、私は一定の間隔で他の画像を読み込むことに打たれました。 – Amarnath

+0

タイマーを試しましたか?必要な間隔に設定することができます。 'System.Windows.Forms.Timer'は、あなたが望むクラスの名前です。あるいは、ツールボックスからフォームにドラッグすることもできます。 –

+0

ちょうどGoogleに行き、「slideshow c#winforms」と入力してください。あなたはうんざりした結果を得るでしょう。スタックオーバーフローの結果が得られるかもしれません。 – King

答えて

3

は最後にそれを得ながらAllaysは永遠にそれを置くスライドショーを維持したい場合は、それが他の人のために役立つことを願っています:

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
string[] images = Directory.GetFiles(@"C:\Dir", "*.jpg"); 
foreach (string image in images) 
{ 
    pictureBox1.Image = new Bitmap(image); 
    Thread.Sleep(5000); 
} 

だけdoWorkイベントでBackgroundWorkerの内側にこのコードを置きます。 あなたがループ

+3

'Thread.Sleep()'?本当に? –

+0

これはイメージをループするアイデアはありませんでしたが、次のようにTimer_Tickイベントを使用してコードを処理しました:pictureBox1.Left = rand.Next(Math.Max(1、Bounds.Width - pictureBox1.Width)); pictureBox1.Top = rand.Next(Math.Max(1、Bounds.Height - pictureBox1.Height)); – Amarnath

+0

イメージイメージ= Image.Fromファイル(images [_counter]); pictureBox1.Width = image.Width; pictureBox1.Height = image.Height; pictureBox1.Image = image; if(_counter Amarnath

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を使用します

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


    } 
+1

説明を追加してください。 –

+0

@OmSaoは要求に応じてコメントを追加しました。 –

+0

Application.DoEvents();大きなフォルダでのメモリリークを避けるためです。ファイルではありません:) –

関連する問題