图像未在Form2中显示

时间:2015-08-19 08:02:13

标签: c#

我试图在特定时间段在form2中显示图像。列表框中有10个图像(如下所示)。图像不显示在form2中为什么?

list box is like
   1.C:\\image1 ,2(sec),3(sec)  -----2sec-displaying the image1 ,3sec Displaying the blank image
   2.C:\\image2 ,3(sec),1(sec)
   3.C:\\image3 ,1(sec),2(sec)
     upto 10 image

Form1.cs的

private void Showbtn_Click(object s, EventArgs e)
   {
     int i=0;
     while(i<listbox.items.count)
       {
          string strpath=listbox1.items[i].Tostring();
          string[] str1= strpth.Split(',');
          f2.updateimage(str1[0]);
          f2.show();
          System.Threading.Thread.Sleep((Convert.Toint32(str1[1]))*1000);
          String Blankimage=@"C://Blankimage.bmp";        
          f2.updateimage(Blankimage) ;
          f2.Show();
          System.Threading.thread.Sleep(Convert.Toint32(str1[2]) * 1000); 
          i++;
         }
    }   

Form2.cs

 public updateimage(string imgpath)
   {
       picturebox1.Load(imgpath);
    }

错误

  imageDisplay.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows       \Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll', Symbols loaded.
'imagedisplay.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll', Symbols loaded.
   thread '<No name>' (0x02fc) has exited with code 0 

3 个答案:

答案 0 :(得分:1)

您必须让主UI在睡觉时更新您的表单(如刷新图片)。要完成此操作,您可以“异步等待”而不是使用Thread.Sleep阻止主线程:

private async void Showbtn_Click(object s, EventArgs e)
{
    int i=0;
    while(i<listbox.items.count)
    {
        string strpath=listbox1.items[i].Tostring();
        string[] str1= strpth.Split(',');
        f2.updateimage(str1[0]);
        f2.show();
        await Task.Delay(Convert.Toint32(str1[1]));
        String Blankimage=@"C://Blankimage.bmp";        
        f2.updateimage(Blankimage) ;
        //f2.Show(); => You don't need that line
        await Task.Delay(Convert.Toint32(str1[2]));
        i++;
    }
} 

答案 1 :(得分:0)

问题是你的while循环主线程忙,所以我们要求它更新form2对象再次绘制它

公共部分类Form1:表单     {         Form2 f2 = new Form2();         公共Form1()         {             的InitializeComponent();         }

    private void button1_Click(object sender, EventArgs e)
    {
        int i = 0;
        f2.Show();
        while (i < listBox1.Items.Count)
        {
            string strpath = listBox1.Items[i].ToString();
            string[] str1 = strpath.Split(',');
            f2.updateimage(str1[0]);
            f2.Update();// Add This Line to make the second form update it self
            System.Threading.Thread.Sleep(5000);
            String Blankimage = @"C://Blankimage.bmp";     
            f2.updateimage(Blankimage);
            f2.Update(); // Add This Line to make the second form update it self
            System.Threading.Thread.Sleep(1000);
            i++;
        }
    }
}

答案 2 :(得分:-1)

System.Threading.Thread.Sleep作为参数有毫秒。因此,您的代码应该可以工作,但图像变化非常快(fe,1.C:\ image1,2(sec),3(sec) - image1将仅显示2毫秒,并且将显示空白图像持续3毫秒)