无法使用form1_Load事件处理程序中的Graphics.DrawImage方法绘制图像。

时间:2015-02-23 11:11:20

标签: c# winforms graphics2d

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Bitmap b = new Bitmap(@"F:\face.jpeg");
   Graphics g = this.CreateGraphics();
        g.DrawImage(b, 20, 30);


    }
}

图片未加载表单。 这是什么原因?而且,我该怎么做?

2 个答案:

答案 0 :(得分:2)

因为它不是在WinForms中绘制的正确方法。将您的绘图代码移动到OnPaint()方法,如下所示:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    e.Graphics.DrawImage(bmp, 20, 30);
}

当然可以提前加载位图。

答案 1 :(得分:1)