C#画图的一些介绍1(GDI+)
namespace _111GDI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//得?到?了?窗洹?体?的?“°画-笔括?”±对?象ó,?其?实害?就í是?窗洹?体?的?表括?面?
Graphics g = this.CreateGraphics();
//g.DrawString("大洙?几?把?", this.Font, Brushes.Black, 200, 400);
g.DrawEllipse(Pens.Blue, 20, 40, 50, 50);//椭圆形
g.DrawRectangle(Pens.Yellow, 20, 40, 50, 50);//长方形
g.Dispose();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.panel1.CreateGraphics();
g.FillEllipse(Brushes.Brown, 50,50,100,100);
g.Dispose();
}
private void Forml_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
//g.Dispose();这里不需要销毁
Pen pen = new Pen(Color.Yellow, 10);//太细,我们重新new个,这里需要销毁
g.DrawLine(pen, 20, 30, 50, 80);//没?需è要癮点?按恪?钮¥就í会á出?来ぁ?这a条?线?,?状痢?体?在ú自?动ˉ绘?制?窗洹?口ú的?时骸?候ò
g.Dispose();
}
private void Form_MouseMove(object sender, MouseEventArgs e)//每?次?移?动ˉ鼠酣?标括?,?其?坐?标括?显?示?到?窗洹?体?拦?上?
{
this.Text=string.Format("x={0},y={1}",e.X,e.Y);
}
}
}
命名空间:System.Drawing;(基本绘图操作)
Graphics对象,可以理解为一只画笔,要在哪个控件上绘图,就要得到对应控件的Graphics对象(就是获得对应控件的绘图表面)。获得Graphics对象一般有两种方式:1.控件.CreateGraphics()//通过该方式创建后要调用g.Dispose()方法释放资源。2.在重写OnPaint()方法中通过参数PaintEventArgs e.Graphics;来获得,一般使用为Paint事件中。//通过这种方式一般不需要手动调用Dispose();区别OnPaint()与窗体的Paint事件。(画一些简单图形)
什么样类型的对象需要使用完成后Dispose()?
答:Pen,Brush,Font,Image,Ico,FileStream(文件操作),网络操作等。 (在这些类中封装了一些操作系统的资源)//Pens.