using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 实例7_7绘制图形
{
public partial class FirstForm : Form
{
public FirstForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.Show();
}
private void button2_Click(object sender, EventArgs e)
{
实例7_8绘制矩形 form8=new 实例7_8绘制矩形 ();
form8.Show();
}
private void button3_Click(object sender, EventArgs e)
{
实例7_9绘制平滑曲线 form9 = new 实例7_9绘制平滑曲线();
form9.Show();
}
private void button4_Click(object sender, EventArgs e)
{
实例7_10绘制贝塞尔曲线 form10 = new 实例7_10绘制贝塞尔曲线();
form10.Show();
}
private void button5_Click(object sender, EventArgs e)
{
实例7_11绘制多边形 form11 = new 实例7_11绘制多边形();
form11.Show();
}
private void button6_Click(object sender, EventArgs e)
{
实例7_12绘制椭圆 form12 = new 实例7_12绘制椭圆();
form12.Show();
}
private void button7_Click(object sender, EventArgs e)
{
实例7_13绘制文字 form13 = new 实例7_13绘制文字();
form13.Show();
}
private void button8_Click(object sender, EventArgs e)
{
实例7_14显示图像 form14 = new 实例7_14显示图像();
form14.Show();
}
private void button9_Click(object sender, EventArgs e)
{
实例7_15刷新图像 form15 = new 实例7_15刷新图像();
form15.Show();
}
private void button10_Click(object sender, EventArgs e)
{
绘制柱形图 formZX = new 绘制柱形图();
formZX.Show();
}
private void button11_Click(object sender, EventArgs e)
{
验证码 yzm = new 验证码();
yzm.Show();
}
private void FirstForm_Load(object sender, EventArgs e)
{
}
}
}
/*
* 使用DrawLine和DrawLines绘制直线。
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 实例7_7绘制图形
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen p = new Pen(Color.Black, 2.5f);
g.DrawLine(p, new Point(50, 10), new Point(250, 10));
Point[] points = { new Point(50,20),new Point(250,20),
new Point(250,70), new Point(50,20)};
g.DrawLines(p, points );
g.Dispose();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
运行结果:
/*
* 使用DrawRectangle、FillRectangle方法绘制、填充矩形轮廓。
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 实例7_8绘制矩形
{
public partial class 实例7_8绘制矩形 : Form
{
public 实例7_8绘制矩形()
{
InitializeComponent();
}
private void 实例7_8绘制矩形_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen p = new Pen(Color.Black, 2.0f);
Rectangle rct1 = new Rectangle(new Point(10, 10), new Size(80, 40));
g.DrawRectangle(p, rct1); //绘制矩形轮廓
//充填矩形区域
Rectangle rct2 = new Rectangle(new Point(100, 80), new Size(200, 50));
SolidBrush brush = new SolidBrush(Color.Blue);
g.FillRectangle(brush, rct2);
g.Dispose();
}
private void 实例7_8绘制矩形_Load(object sender, EventArgs e)
{
this.Text = "实例7-8绘制矩形";
}
}
}
运行结果:
/*
* 使用DrawCurve方法绘制平滑曲线。
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 实例7_7绘制图形
{
public partial class 实例7_9绘制平滑曲线 : Form
{
public 实例7_9绘制平滑曲线()
{
InitializeComponent();
}
private void 实例7_9绘制平滑曲线_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
//绘制平滑曲线
Pen p = new Pen(Color.Black, 2.0f);
Point[] points = {new Point(50,100), new Point(100, 20),
new Point(200, 100), new Point(250, 20), new Point(300, 75) };
g.DrawCurve(p, points, 0.8f);
g.Dispose();
}
private void 实例7_9绘制平滑曲线_Load(object sender, EventArgs e)
{
}
}
}
运行结果;
//绘制贝塞尔曲线
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 实例7_7绘制图形
{
public partial class 实例7_10绘制贝塞尔曲线 : Form
{
public 实例7_10绘制贝塞尔曲线()
{
InitializeComponent();
}
private void 实例7_10绘制贝塞尔曲线_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
//绘制贝塞尔曲线
Pen p = new Pen(Color.Black, 2.0f);
Point p1 = new Point(50, 100);
Point p2 = new Point(100, 20);
Point p3 = new Point(200, 200);
Point p4 = new Point(250, 80);
g.DrawBezier(p, p1, p2, p3, p4);
g.Dispose();
}
private void 实例7_10绘制贝塞尔曲线_Load(object sender, EventArgs e)
{
}
}
}
运行结果:
//绘制、填充多边形
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 实例7_7绘制图形
{
public partial class 实例7_11绘制多边形 : Form
{
public 实例7_11绘制多边形()
{
InitializeComponent();
}
private void 实例7_11绘制多边形_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
//绘制多边形轮廓
Pen p = new Pen(Color.Black, 2.0f);
g.DrawPolygon(p, new Point[]{
new Point(20,20),
new Point(120,20),
new Point(70,70),
new Point(20,20)});
//充填多边形区域
SolidBrush brush = new SolidBrush(Color.Blue);
Point[] points = {new Point(150,70), new Point(250,70),
new Point(200,20), new Point(150,70) };
g.FillPolygon(brush, points );
g.Dispose();
}
private void 实例7_11绘制多边形_Load(object sender, EventArgs e)
{
}
}
}
运行结果:
//绘制椭圆
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 实例7_7绘制图形
{
public partial class 实例7_12绘制椭圆 : Form
{
public 实例7_12绘制椭圆()
{
InitializeComponent();
}
private void 实例7_12绘制椭圆_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen p = new Pen(Color.Black, 2.0f);
g.DrawEllipse(p, new Rectangle(30, 30, 100, 60)); //绘制椭圆
SolidBrush brush = new SolidBrush(Color.Blue);
g.FillEllipse(brush, new Rectangle(180, 60, 100, 60)); //填充椭圆
g.Dispose();
}
private void 实例7_12绘制椭圆_Load(object sender, EventArgs e)
{
}
}
}
运行结果:
//绘制文字,该文字由图片填充。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D ;
namespace 实例7_7绘制图形
{
public partial class 实例7_13绘制文字 : Form
{
public 实例7_13绘制文字()
{
InitializeComponent();
}
private void 实例7_13绘制文字_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Image image = Image.FromFile(Application.StartupPath + @"\p01.jpg");
TextureBrush brush = new TextureBrush(image );
//创建字体
Font font = new Font("黑体", 60, FontStyle.Underline ^ FontStyle.Bold);
g.DrawString("烟台大学", font, brush, new Point(10, 10));
g.Dispose();
}
private void 实例7_13绘制文字_Load(object sender, EventArgs e)
{
}
}
}
运行结果:
/*
* 显示图像和保存图像。
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 实例7_7绘制图形
{
public partial class 实例7_14显示图像 : Form
{
public 实例7_14显示图像()
{
InitializeComponent();
}
Graphics g;
Bitmap bitmap ;
private void button1_Click(object sender, EventArgs e)
{
g = pictureBox1.CreateGraphics();
bitmap = new Bitmap(Application.StartupPath + @"\p01.jpg");
g.DrawImage(bitmap, 0, 0); //从PictureBox1控件的左上角开始绘制
bitmap.Dispose();
g.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
bitmap = new Bitmap(Application.StartupPath + @"\p01.jpg ");
Bitmap image = new Bitmap(pictureBox1.Width ,pictureBox1.Height ); //以图片框的高度和宽度保存图像
g = Graphics.FromImage(image);
g.DrawImage(bitmap, 0,0);
image.Save(@"D:\p01.gif", System.Drawing.Imaging.ImageFormat.Gif);
bitmap.Dispose();
image.Dispose();
g.Dispose();
}
private void 实例7_14显示图像_Load(object sender, EventArgs e)
{
}
}
}
运行结果:
//刷新图像。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 实例7_7绘制图形
{
public partial class 实例7_15刷新图像 : Form
{
public 实例7_15刷新图像()
{
InitializeComponent();
}
Bitmap bitmap;
Graphics g;
private void button1_Click(object sender, EventArgs e)
{
bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.BackgroundImage = bitmap;
g = Graphics.FromImage(bitmap);
g.Clear(pictureBox1.BackColor);
Pen pen=new Pen (Color.Red ,3.0f );
Point p1 = new Point(10, 10);
Point p2 = new Point(150, 100);
g.DrawLine(pen, p1, p2);
g.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.BackgroundImage = bitmap;
g = Graphics.FromImage(bitmap);
g.Clear(pictureBox1.BackColor);
Pen pen = new Pen(Color.Red, 3.0f);
Rectangle rec = new Rectangle(0, 0, 100, 100);
g.DrawEllipse(pen, rec);
g.Dispose();
}
private void 实例7_15刷新图像_Load(object sender, EventArgs e)
{
}
}
}
运行结果: