C#实验7 GDI+编程

【实验目的】

1.理解Graphics对象概念,并熟悉Graphics对象的创建方法。

2.掌握利用Graphics对象绘制线条和形状方法。

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

4.掌握利用Graphics对象DrawImage()显示图像方法。

【实验内容】

1、编写一个Windows应用程序,实现窗体自上而下变的背景。

 

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像素的蓝色圆。该圆在窗体范围内左右移动,并在圆内显示圆心相对于窗体的坐标。

 

 

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,大家也可以自行寻找其它图片。

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.画线实验。创建一个窗体,在窗体上添加一个按钮(按钮标题为“选择画笔颜色”)。窗体界面如下图所示。单击“选择画笔颜色”按钮,能够打开一个颜色对话框,选择颜色,进而更改画笔颜色。使用画笔可以在窗体上画任意的曲线。

 

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;//初始化鼠标点击落点
        }

    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值