天天看點

C#實驗7 GDI+程式設計

【實驗目的】

1.了解Graphics對象概念,并熟悉Graphics對象的建立方法。

2.掌握利用Graphics對象繪制線條和形狀方法。

3.掌握利用Graphics對象的DrawString()呈現文本方法。

4.掌握利用Graphics對象DrawImage()顯示圖像方法。

【實驗内容】

1、編寫一個Windows應用程式,實作窗體自上而下變的背景。

C#實驗7 GDI+程式設計
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
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, Color.White, Color.Green, LinearGradientMode.Vertical);
            g.FillRectangle(brush, this.ClientRectangle);
            g.Dispose();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
           

2、編寫一個Windows應用程式。當窗體加載後,呈現一個半徑為100像素的藍色圓。該圓在窗體範圍内左右移動,并在圓内顯示圓心相對于窗體的坐标。

C#實驗7 GDI+程式設計
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
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
       private void Form2_Paint(object sender, PaintEventArgs e)
        {
            Graphics g=e.Graphics;
     
            GraphicsPath path = new GraphicsPath();
            int R = 60;
            Random rd = new Random();
            int X = rd.Next(0, this.ClientSize.Width - 2*R);
            int Y = rd.Next(0, this.ClientSize.Height - 2*R);
            Point centerPoint = new Point(X, Y);;
            path.AddEllipse(centerPoint.X - R, centerPoint.Y - R, 2 * R, 2 * R);
            PathGradientBrush brush = new PathGradientBrush(path);
            brush.CenterPoint = centerPoint;//指定路徑中心點
            brush.CenterColor = Color.White;
            brush.SurroundColors = new Color[] { Color.Black };
            g.FillEllipse(brush,centerPoint.X-R,centerPoint.Y-R,2*R,2*R);
            g.Dispose();
            label1.Location = new Point(centerPoint.X, centerPoint.Y);
            label1.Text = "x=" + (centerPoint.X + R) + ",y=" + (centerPoint.Y + R);
          
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            label1.Visible = true;
            label1.BackColor = Color.White;
            label1.ForeColor = Color.Red;
        }
      
    }
}
           

3.編寫一個Windows應用程式。利用Graphics對象的DrawString方法在窗體上繪制文字“煙台大學”,要求文字用一幅圖檔填充。給出圖檔名稱p1.jpg,大家也可以自行尋找其它圖檔。

C#實驗7 GDI+程式設計
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
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Image image = Image.FromFile(Application.StartupPath + @"\p1.jpg");
            TextureBrush brush = new TextureBrush(image);
            Font font = new Font("黑體", 60, FontStyle.Underline ^ FontStyle.Bold);
            g.DrawString("煙台大學", font, brush, new Point(100, 100));
            g.Dispose();
        }

        private void Form3_Load(object sender, EventArgs e)
        {

        }
    }
}
           

4.畫線實驗。建立一個窗體,在窗體上添加一個按鈕(按鈕标題為“選擇畫筆顔色”)。窗體界面如下圖所示。單擊“選擇畫筆顔色”按鈕,能夠打開一個顔色對話框,選擇顔色,進而更改畫筆顔色。使用畫筆可以在窗體上畫任意的曲線。

C#實驗7 GDI+程式設計
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
{
    public partial class Form4 : Form
    {
        Pen pen = null;
        Graphics g = null;
        Point p0, p1;
        public Form4()
        {
            InitializeComponent();
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            pen = new Pen(Color.Red);
            g = this.CreateGraphics();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            pen.Color = colorDialog1.Color;
        }

        private void Form4_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                p0 = p1;//p0作為點選起點
                p1 = e.Location;//p1作為點選落點
                g.DrawLine(pen, p0, p1);
            }
        }

        private void Form4_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left) p1 = e.Location;//初始化滑鼠點選落點
        }

    }
}
           
c#