GDI+就是用来画图的;炒股软件,心电图等都是GDI画的,GDI不是.net的技术,他是一个单独的技术
private void Form1_Load(object sender, EventArgs e)
{
//画图需要一根笔,颜色 一张纸,两点,绘制直线的对象
//纸就是窗体,绘制直线的对象就是GDI对象
}
private void button1_Click(object sender, EventArgs e)
{
//创建GDI对象;
Graphics g = this.CreateGraphics();//通过this当前窗体创建对象//new Graphics();
//创建画笔对象
Pen pen = new Pen(Brushes.YellowGreen);
//创建两个点
Point p1 = new Point(30, 50);
Point p2 = new Point(250, 250);
g.DrawLine(pen, p1, p2);
//当我们把窗体从一个地方移动到另外一个地方的时候,操作系统把原来那个擦掉了,重新画了一个新的;
//当我们画了线的窗体拖出窗体的时候,窗体会被操作系统重新画一遍,但是直线不会被操作系重新画一遍,因此直线会消失
}
int i = 0;
private void Form1_Paint(object sender, PaintEventArgs e) //重绘窗体的时候执行的事件;
{
i++;
label1.Text = i.ToString(); //记录被重绘的次数;
//创建GDI对象;
Graphics g = this.CreateGraphics();//通过this当前窗体创建对象//new Graphics();
//创建画笔对象
Pen pen = new Pen(Brushes.YellowGreen);
//创建两个点
Point p1 = new Point(30, 50);
Point p2 = new Point(250, 250);
g.DrawLine(pen, p1, p2);
}
//要看方法重载的参数,方法名后面打办个括号,按上下方向键;
private void button2_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Brushes.Red);
Size size = new Size(80,80);
Rectangle rec = new Rectangle(new Point(50,50),size);
g.DrawRectangle(pen, rec);
}
private void button3_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Brushes.Blue);
Size size = new Size(180,180);
Rectangle rec = new Rectangle(new Point(150,150),size);
g.DrawPie(pen, rec, 60, 60);
}
private void button4_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.DrawString("美丽可爱的球球", new Font("宋体", 20, FontStyle.Underline), Brushes.Gold, new Point(300, 300));
}
系统里面的图像默认是位图;
--------画验证码;
private void pictureBox1_Click(object sender, EventArgs e)
{
Random r = new Random();
string str = null;
for (int i = 0; i < 5; i++)
{
int rnum = r.Next(0, 10);
str += rnum;
}
//创建GDI对象;
Bitmap bmp = new Bitmap(120,40);
Graphics g = Graphics.FromImage(bmp); //在bmp上画
for (int i = 0; i < 5; i++)
{
string[] font = {"微软雅黑","宋体","黑体","隶书","仿宋"};
Color[] color = {Color.Red,Color.Yellow,Color.Blue,Color.Green,Color.Pink,Color.Purple,Color.Black,Color.Chocolate};
//new SolidBrush单色画笔,参数传Color,最后一个参数点的坐标不能随机给,随机给会重叠
Point p = new Point(i*15,0); //这个点坐标不是相对于窗体,而是bitmap;
g.DrawString(str[i].ToString(),new Font(font[r.Next(0,5)],30,FontStyle.Bold),new SolidBrush(color[r.Next(0,8)]),p);
}
//给图像画上干扰线;
for (int i = 0; i < 30; i++)
{
//直线的点的坐标必须在bmp里面
Point p1 = new Point(r.Next(0,bmp.Width),r.Next(0,bmp.Height));
Point p2 = new Point(r.Next(0,bmp.Width),r.Next(0,bmp.Height));
g.DrawLine(new Pen(Brushes.Green),p1,p2);
}
//画像素颗粒就不用GDI了,而是直接设置bmp像素的颜色;
for (int i = 0; i < 1000; i++)
{
//点的坐标必须在bmp里面
Point p = new Point(r.Next(0,bmp.Width),r.Next(0,bmp.Height));
//指定图片中像素颜色
bmp.SetPixel(p.X, p.Y, Color.DarkOliveGreen);
}
//将图片镶嵌到picturebox中,
pictureBox1.Image = bmp;
}
//第一步产生随机数;第二步画字符串,第三步画线,第四步画点;