可能重复:
如何修复用户控件中的闪烁
我目前正在开发一个程序,该程序可以为表单的背景图像动态加载图像。一旦图像完全应用于背景,我希望表单可见。
我已经尝试过表单事件BackgroundImageChanged
,但这没有用。
如果图像是高分辨率的,我可以看到表格出现并且图像仍在自行构建。
代码片段:
this.backgroundImage = loadedImage;
// Thread.Sleep(5000);
this.Show();
正如您所看到的,我正在使用Thread.Sleep()
一些时间来在显示表单之前完全加载图像。但是有没有这样的方法:
this.backgroundImage = loadedImage;
while( imageIsNotFullyLoaded() ) { }
this.Show();
或者有没有其他方法或事件我还没有见过?
编辑:
我的代码:
forn1.cs :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace GameClient {
public partial class FormMain:Form {
getImage _getImage;
public FormMain() {
InitializeComponent();
}
private void FormMain_Load( object sender, EventArgs e ) {
_getImage = new getImage();
Image splashLogo = _getImage.fromURL( "http://ace-acid.no-ip.org/GameClient/splash.png" );
this.BackgroundImage = splashLogo;
Thread.Sleep(3000);
this.Show();
}
private void btnLogin_Click( object sender, EventArgs e ) {
this.Hide();
}
}
}
获取图像.cs:
using System;
using System.Drawing;
using System.IO;
using System.Net;
namespace GameClient {
class getImage {
public Image fromURL( string URL ) {
Image tmpImg = null;
try {
HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create( URL );
request.AllowWriteStreamBuffering = true;
request.Timeout = 20000;
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
tmpImg = Image.FromStream( stream );
response.Close();
} catch( Exception ex) {
return null;
}
return tmpImg;
}
}
}