C# winform 初始化图像问题

来源:百度知道 编辑:UC知道 时间:2024/06/01 02:42:16
我想在调用一个页面的时候初始化显示图像。
代码如下
Graphics gx=null;
Bitmap img=null;
private void Form2_Load(object sender, EventArgs e)
{
img = Properties.Resources.Loading;
this.gx = this.CreateGraphics();
gx.DrawImage(img, 0, 0);
Pen pen = new Pen(Color.Red, 2);
gx.DrawLine(pen, 100, 100, 200, 200);
}
调试时没反应,但是我把该代码放到button click事件里却有作用 private void button1_Click_1(object sender, EventArgs e)
{
img = Properties.Resources.Loading;
this.gx = this.CreateGraphics();
gx.DrawImage(img, 0, 0);
Pen pen = new Pen(Color.Red, 2);
gx.DrawLine(pen, 100, 100, 200, 200);
}

不知道为什么,请强人解决下!

button1_Click_1 可以用,但是窗体刷新后, 你画的东西肯定不在了。

所以这段代码需要添加到OnPaint中,你可以重载这个方法, 也可以注册相应的事件。

请为form添加Form2_Load事件。