烟台大学 .net 实验7 GDI+编程

烟台大学 .net 实验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 test7
{
    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();
        }
    }
}

在这里插入图片描述
//
///
//
/

//
/

///

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;

namespace test7
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen pen = new Pen(Color.Gray, 1);
            int r = 80;
            Random rd = new Random();
            int X = rd.Next(0, this.ClientSize.Width - 2*r);
            int Y = rd.Next(0, this.ClientSize.Height - 2*r);
            int x1, x2;
            x1 = X; x2 = Y;
            g.DrawEllipse(pen, x1, x2, 2 * r, 2 * r);
            SolidBrush brush = new SolidBrush(Color.Blue);
            g.FillEllipse(brush, x1, x2, 2 * r, 2 * r);
            label1.Location = new Point(x1 + r, x2 + r);
            label1.Text = "x=" + (x1+r) + ",y=" +( x2+r);
        }

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

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 test7
{
    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(10, 10));
            g.Dispose();
        }
    }
}

//
//
//
//
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 test7
{
    public partial class Form4 : Form
    {
        Pen pen = new Pen(Color.Blue);
        Point point1, point2;
        Graphics graphics = null;
        public Form4()
        {
            InitializeComponent();
        }

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

         private void Form4_MouseDown(object sender, MouseEventArgs e)
         {
             if (e.Button == MouseButtons.Left)
                 point2 = e.Location;
         }

         private void Form4_MouseMove(object sender, MouseEventArgs e)
         {
             if (e.Button == MouseButtons.Left)
             {
                 point1 = point2;
                 point2 = e.Location;
                 graphics.DrawLine(pen, point1, point2);
             }
         }

         private void Form4_Paint(object sender, PaintEventArgs e)
         {
             graphics = this.CreateGraphics();
         }

         private void Form4_Load(object sender, EventArgs e)
         {
             button1.Text = "选择画笔颜色";
         }


    }
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值