图片框不显示图像

SamDroid

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

更新

我在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表单,则此后会显示图像!

问题

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

伯恩德·林德

问题似乎出在,您的BmForm准备工作正在锁定试图加载初始图像以及处理命令以准备BmForm主UI线程
为了解决这个问题,请在其自己的线程中加载启动窗体,并在完成加载后关闭线程/窗体。

代码示例:

在您的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。

您可以在此处阅读有关线程的更多信息

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章