C#中的g.DrawImage问题,请教一下,为什么ShowPic()不显示图片?

来源:百度知道 编辑:UC知道 时间:2024/05/26 10:32:47
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ShowPic();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{

Bitmap bmp = new Bitmap("e:/03.jpg");
Graphics g = this.CreateGraphics();
//画像描绘
g.Clear(Color.White);
g.DrawImage(bmp, 0, 0, 62, 71);
}
private void ShowPic()
{
Bitmap bmp = new Bitmap("e:/03.jpg");
Graphics g = this.CreateGraphics();
//画像描绘
g.Clear(Color.White);
g.DrawImage(bmp, new System.Drawing.Rectangle(70,70, 62, 71));
}
}
是不是有关DrawImage的都要在Form1_Paint下才有效?例如我把ShowPic()函数加到这下面,就可以出图像。还有其他方法吗?

由于Winform是不断重绘的,包括最大化、最小化、移动窗体等操作,Winform都会不断的重绘自己,所以在Load时画图,只闪现了一下,然后又被重绘了。
在Paint里做到画图,意思就是重绘时都调用你的绘图方式,所以可以正常显示。
有点需要指出的:在你的Paint方法里,Graphics可以不由CreateGraphics产生,可以由参数e里的Graphics

public Form1()
{
InitializeComponent();
ShowPic();
}
你这样写、?
那初始化都没有完成···画不了

就写在Paint里吧
要是要重新调用,就直接this.Refresh()