標籤:
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) { } }}運行結果:
C#—繪製圖形