C# Graphics类对象创建方式

1.通过窗体或控件的Paint事件进行创建

Graphics g = e.Graphics;

private void button1_Paint(object sender, PaintEventArgs e)
        {
            //通过控件Paint事件为当前控件创建Graphics对象
            Graphics g = e.Graphics;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //通过窗体Paint事件为当前窗体创建Graphics对象
            Graphics u = e.Graphics;
        }

2.调用当前窗体或控件的CreateGraphics()方法

Graphics pp = this.CreateGraphics();

Graphics p =button1.CreateGraphics();

private void button1_Click(object sender, EventArgs e)
        {
            //调用当前窗体的CreateGraphics()方法为窗体创建Graphics对象;
            Graphics pp = this.CreateGraphics();
            //调用当前控件的CreateGraphics()方法为控件创建Graphics对象;
            Graphics p =button1.CreateGraphics();

3.调用Graphics类的静态方法FromImage()创建

例如:Image img=Image.FromFile(1.gif);

         Graphics p= Graphics.FromImage(img);

你可能感兴趣的:(c#)