winfrom 循環播放圖片

沒啥新東西了,就是遍歷和匹配文件名而後獲取對象,放到picturebox裏面spa

選中listview中想要查看的圖片,而後點擊查看按鈕,進行↓代碼。線程

if (listView1.SelectedItems.Count > 0)
            {
                string name = listView1.Items[listView1.SelectedIndices[0]].Text;//選中的文件名
                string dpath = fName.Substring(0, fName.LastIndexOf("\\"));//去除掉文件名
                DirectoryInfo dti = new DirectoryInfo(dpath); FileInfo[] file = dti.GetFiles(name);//查找名稱和name同樣的圖片 Bitmap bmp = new Bitmap(file[0].FullName); pictureBox1.Image = bmp; pictureBox1.Refresh(); getgridv(name); } else { MessageBox.Show("請先選中須要查看的圖片"); } 

循環播放

循環播放用的線程執行,由於用進程執行的話,他會一直執行圖片的循環而沒法進行其餘操做。code

在方法中啓動線程執行方法,須要在構造中加上Control.CheckForIllegalCrossThreadCalls = false;對象

而後就是點擊按鈕,建立並開啓線程blog

if (run==null)
{
  Thread td = new Thread(xunhuan);
  td.Start();
}進程

      int i = 0;
        bool bf = true;//判斷是否暫停播放
        string run = null;//用來判斷線程是否在運行
        public  void xunhuan()
        {
            run = "run";
            if (listView1.SelectedItems.Count > 0)//若是listview有選中項就從選中項開始循環
            {                
                while (true)
                {
                    if (!bf)
                    {
                        break;
                    }
                    if (i >= listView1.Items.Count)
                    {
                        i = 0;
                        break;
                    }
                    if (i >= listView1.SelectedIndices[0])
                    {
                        cycle(i);//加載圖片
                        System.Threading.Thread.Sleep(300);
                    }
                    i++;
                }
            }
            if (i == 0)
            {
                int j = 0;
                while (true)
                {
                    if (!bf)
                    {
                        break;
                    }
                    if (j >= listView1.Items.Count)
                    {
                        j=0;
                    }
                    //listView1.Items[j].Focused = true;
                    cycle(listView1.Items[j].Text);
                    System.Threading.Thread.Sleep(300);
                    j++;
                }
            }
        }

還有一個倒敘循環,和上面基本同樣,也是經過線程執行,判斷bool是否暫停。圖片

private void daoxu()
{
  run = "daorun";
  int dx = listView1.Items.Count;
  if (listView1.SelectedItems.Count > 0)//若是listview有選中項就從選中項開始循環
  {
    while (true)
    {
      if (!bf)
      {
        break;
      }
      if (dx < 0)
      {
        break;
      }
      if (dx <= listView1.SelectedIndices[0])
      {
        cycle(dx);//加載圖片
        System.Threading.Thread.Sleep(300);
      }
      dx--;
    }
  }
  if (dx == listView1.Items.Count)
  {
    int j = listView1.Items.Count - 1;
    while (true)
    {
      if (!bf)
      {
        break;
      }
      if (j < 0)
      {
      break;
      }
      //listView1.Items[j].Focused = true;
      cycle(listView1.Items[j].Text);
      System.Threading.Thread.Sleep(300);
      j--;
    }
  }
}get

 

點擊更改bf屬性來更改播放狀態string

if (bf)
{
bf = false;//中止
btn_pause.Text = "中止";
}
else
{
bf = true;//播放
btn_pause.Text = "播放";
run = null;
}it

相關文章
相關標籤/搜索