C# 图片框没有';不显示图像
我在Vs2010中有一个表单项目。 这是我的设想: 我创建了一个表单,我想像启动屏幕一样使用它,没有边框。里面有一个像表格一样大的图片框。 我设置了导入它的图像,在designer中我可以看到它。 但是当我从另一个窗体调用splashscreen窗体并显示它时,我只能看到图片框边框,但无法加载图像 更新 我以BMU形式加载防溅屏(其他形式): 这是闪屏形式的图片框的设计器代码片段:C# 图片框没有';不显示图像,c#,image,forms,picturebox,splash-screen,C#,Image,Forms,Picturebox,Splash Screen,我在Vs2010中有一个表单项目。 这是我的设想: 我创建了一个表单,我想像启动屏幕一样使用它,没有边框。里面有一个像表格一样大的图片框。 我设置了导入它的图像,在designer中我可以看到它。 但是当我从另一个窗体调用splashscreen窗体并显示它时,我只能看到图片框边框,但无法加载图像 更新 我以BMU形式加载防溅屏(其他形式): 这是闪屏形式的图片框的设计器代码片段: this.pictureBox1.BorderStyle = System.Windows.Forms.Borde
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表单,则在此之后显示图像
问题
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();
}
}
}
有人知道为什么图片没有显示出来吗?问题似乎是BmForm的准备过程锁定了主UI线程,该线程试图加载启动图像并处理命令以准备BmForm。
要解决这个问题,请将splash表单加载到它自己的线程中,并在加载完成后关闭线程/表单 代码示例: 在您的BMU表单中加载
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();
}
}
}
您可能需要使用System.Threading添加向类中添加代码>,并在发生错误时向BmForm_Load事件中添加一些额外的错误处理,以便清理splash线程
您可以阅读有关线程的更多信息是否将图像作为资源导入项目或直接链接到图像文件?@BerndLinde我已将图像作为资源导入,但如果我直接链接图像文件(我尝试了多个图像),同样的问题仍然存在你能展示一下你用来显示来自另一个表单的表单的代码和pictureBox的设计器代码片段吗?@BerndLinde我更新了这个问题!谢谢