图片框不显示图像

时间:2014-10-22 12:12:35

标签: c# image forms picturebox splash-screen

我在Vs2010中有一个表单项目。 这是我的情景: 我创建了一个我想要使用的表单,如启动画面,没有边框。在里面,我有一个像形式一样大的图片框。 我设置导入它的图像,在设计器中我可以看到它。 但是,当我从另一个表单调用splashscreen表单并显示它时,我只能看到图片框边框,但不会加载图像。

更新

我在BmForm_Load(其他形式)中加载splashScreen:

SplashScreen ss = new SplashScreen();
ss.TopMost = true;
ss.Show();
//Prepare bmForm....
ss.Close();

这是以闪屏形式显示的图片框的设计器代码段:

this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.ImageLocation = "";
this.pictureBox1.InitialImage = null;
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(256, 256);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.WaitOnLoad = true;

更新2

如果我在其他表单加载结束之前没有关闭splashScreen表单,则会在此之后显示图像!

问题

有人知道为什么图片不显示?

1 个答案:

答案 0 :(得分:1)

问题似乎是你的BmForm的准备锁定了试图加载启动图像的主UI线程以及处理准备BmForm的命令 要解决这个问题,请在自己的线程中加载splash表单,并在加载完成后关闭线程/表单。

代码示例:

在BmForm_Load中

Thread splashThread = new Thread(ShowSplash);
splashThread.Start();

// Initialize bmForm

splashThread.Abort();
// This is just to ensure that the form gets its focus back, can be left out.
bmForm.Focus();

显示启动画面的方法

private void ShowSplash()
{
  SplashScreen splashScreen = null;
  try
  {
    splashScreen = new SplashScreen();
    splashScreen.TopMost = true;
    // Use ShowDialog() here because the form doesn't show when using Show()
    splashScreen.ShowDialog();
  }
  catch (ThreadAbortException)
  {
    if (splashScreen != null)
    {
      splashScreen.Close();
    }
  }
}

您可能需要将using System.Threading;添加到您的类中,并在发生错误时向BmForm_Load事件添加一些额外的错误处理,以便您可以清理splashThread。

您可以阅读有关线程here

的更多信息
相关问题