C#图形图像编程实验

实验目标:

  1. 掌握C#图形绘制基本概念;
  2. 掌握C#字体处理;
  3. 能进行C#图形图像综合设计。

核心功能:

  1. 使用pen绘制基础图形;
  2. 使用LinearGradientBrush实现渐变色字体;
  3. 使用GraphicsPath实现艺术字。

代码实现:

功能1:

设计界面添加button。

 private void button1_Click(object sender, EventArgs e)
        {
            Graphics graphics = this.CreateGraphics();       //创建Graphics对象
            Pen myPen = new Pen(Color.Blue, 2);              //创建Pen对象
            graphics.DrawEllipse(myPen, 80, 150, 100, 50);   //绘制椭圆
            graphics.DrawRectangle(myPen, 80, 150, 100, 50); //绘制矩形
        }

通过 Graphics 类中的 DrawEllipse 方法可以轻松地绘制椭圆。该方法可以绘制由一对坐标、高度和宽度指定的椭圆。 语法如下:

public void DrawEllipse(Pen pen,int x,int y,int width,int height)

矩形绘制(DrawRectangle)同理 。

功能2:

using System.Drawing.Drawing2D;

private void button1_Click(object sender, EventArgs e)
        {
            Graphics graphics = this.CreateGraphics();       
            Pen myPen = new Pen(Color.Blue, 2);     
            graphics.DrawEllipse(myPen, 80, 150, 100, 50);
            graphics.DrawRectangle(myPen, 80, 150, 100, 50);

            Font font = new Font("微软雅黑", 20);           
            string str = "大家学会了吗???";                                       //设置字符串
            SizeF size = graphics.MeasureString(str, font);                         //获取字符串的尺寸
            PointF point = new PointF(80, 50);
            RectangleF rect = new RectangleF(point, size);                          //字符串显示的区域
            LinearGradientBrush linearBrush = new LinearGradientBrush(rect, 
                Color.GreenYellow, Color.OrangeRed, LinearGradientMode.Horizontal); //创建笔刷
            graphics.DrawString(str, font, linearBrush, point);                     //绘制文本
        }

LinearGradientBrush(Rectangle, Color, Color, LinearGradientMode)

 功能3:

private void button2_Click(object sender, EventArgs e)
        {
            Graphics graphics = this.CreateGraphics();
            Pen myPen = new Pen(Color.Blue, 2);

            /*下面即老师所给的核心代码,稍作修改
            GraphicsPath gp = new GraphicsPath(FillMode.Winding);
            gp.AddString(
                "字体轮廓",new FontFamily("方正舒体"),(int)FontStyle.Regular,
                80,new PointF(10, 20),new StringFormat());
            Brush brush = XXXXXXXXXXXXXXXXXXXXXX;
            XXX.DrawPath(Pens.Green, gp);
            XXX.FillPath(brush, gp);
            */

            GraphicsPath gp = new GraphicsPath(FillMode.Winding);
            gp.AddString(
                this.textBox1.Text, new FontFamily("方正舒体"), (int)FontStyle.Regular,
                50, new PointF(250, 150), new StringFormat());
            //text Box1.Text的值就是用户输入的字符串
            Brush brush = Brushes.OrangeRed;  
            graphics.DrawPath(Pens.Green, gp);
            graphics.FillPath(brush, gp);
        }

AddString(String, FontFamily, Int32, Single, PointF, StringFormat)

结果展示:

相关参考:

C# 基本图形绘制(二) (qq.com)

 https://www.dresnclor.com/?p=496

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值