C#图形图像编程

一.实验目标和实验内容:

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

运行效果如下所示:

二.准备

(1)创建一个新项目:

(2)在工具箱中拖出四个控件,button1,button2,panel,textbox(可单击控件在右下角修改名字)

三.代码实现

(1)button1

 private void button1_Click(object sender, EventArgs e)
 {
     drawBasicShapes = true;
     drawArtText = false;
     DrawBasicShapes();
     panel1.Invalidate(); // 触发重绘
 }

(2)button2

private void button2_Click(object sender, EventArgs e)
{
    drawBasicShapes = false;
    drawArtText = true;
    DrawArtText();
    panel1.Invalidate(); // 触发重绘
}

(3)panel

private void panel1_Paint(object sender, PaintEventArgs e)
{
    // 将 Bitmap 绘制到 Panel 上
    e.Graphics.DrawImage(drawingBitmap, 0, 0);
}

(4)绘制基础图形

private void DrawBasicShapes()
{
    // 使用 Pen 绘制基础图形
    Pen blackPen = new Pen(Color.Black, 3);
    drawingGraphics.DrawRectangle(blackPen, 10, 70, 100, 50);
    drawingGraphics.DrawEllipse(blackPen, 10, 70, 100, 50);

    // 使用 LinearGradientBrush 实现渐变色字体
    Font font = new Font("Arial", 24);
    LinearGradientBrush gradientBrush = new LinearGradientBrush(
        new Rectangle(10, 20, 100, 50),
        Color.Blue,
        Color.Red,
        45);
    drawingGraphics.DrawString("学会了", font, gradientBrush, new PointF(10, 10));
}

(5)绘制艺术字

private void DrawArtText()
{
    string text = textBox1.Text;
    using (Font font = new Font("方正舒体", 40, FontStyle.Regular))
    using (GraphicsPath gp = new GraphicsPath(FillMode.Winding))
    {
        gp.AddString(
            text,
            font.FontFamily,
            (int)font.Style,
            font.Size,
            new PointF(150, 50), // 设置艺术字的起始位置  
            StringFormat.GenericDefault);

        // 使用 SolidBrush 填充艺术字
        using (SolidBrush brush = new SolidBrush(Color.Blue))
        {
            drawingGraphics.FillPath(brush, gp); // 填充艺术字
        }

        // 使用 Pen 绘制艺术字的轮廓
        using (Pen pen = new Pen(Color.Blue, 2))
        {
            drawingGraphics.DrawPath(pen, gp); // 绘制艺术字的轮廓
        }
    }
}

四.完整代码

using System.Drawing.Drawing2D;
using System.Drawing;
using System.Windows.Forms;
using System;

namespace 图形图像编程
{
    public partial class Form1 : Form
    {
        // 标志位
        private bool drawBasicShapes = false;
        private bool drawArtText = false;

        // Bitmap 对象,用于保存绘制的图像
        private Bitmap drawingBitmap;
        private Graphics drawingGraphics;

        // 构造函数
        public Form1()
        {
            InitializeComponent();

            // 初始化 Bitmap 和 Graphics 对象
            drawingBitmap = new Bitmap(panel1.Width, panel1.Height);
            drawingGraphics = Graphics.FromImage(drawingBitmap);

            // 绑定按钮点击事件
            button1.Click += new EventHandler(this.button1_Click);
            button2.Click += new EventHandler(this.button2_Click);
            panel1.Paint += new PaintEventHandler(this.panel1_Paint);
        }

        // 绘制基础图形按钮点击事件处理
        private void button1_Click(object sender, EventArgs e)
        {
            drawBasicShapes = true;
            drawArtText = false;
            DrawBasicShapes();
            panel1.Invalidate(); // 触发重绘
        }

        // 添加艺术字按钮点击事件处理
        private void button2_Click(object sender, EventArgs e)
        {
            drawBasicShapes = false;
            drawArtText = true;
            DrawArtText();
            panel1.Invalidate(); // 触发重绘
        }

        // 绘制基础图形的方法
        private void DrawBasicShapes()
        {
            // 使用 Pen 绘制基础图形
            Pen blackPen = new Pen(Color.Black, 3);
            drawingGraphics.DrawRectangle(blackPen, 10, 70, 100, 50);
            drawingGraphics.DrawEllipse(blackPen, 10, 70, 100, 50);

            // 使用 LinearGradientBrush 实现渐变色字体
            Font font = new Font("Arial", 24);
            LinearGradientBrush gradientBrush = new LinearGradientBrush(
                new Rectangle(10, 20, 100, 50),
                Color.Blue,
                Color.Red,
                45);
            drawingGraphics.DrawString("学会了", font, gradientBrush, new PointF(10, 10));
        }

        // 绘制艺术字的方法
        private void DrawArtText()
        {
            string text = textBox1.Text;
            using (Font font = new Font("方正舒体", 40, FontStyle.Regular))
            using (GraphicsPath gp = new GraphicsPath(FillMode.Winding))
            {
                gp.AddString(
                    text,
                    font.FontFamily,
                    (int)font.Style,
                    font.Size,
                    new PointF(150, 50), // 设置艺术字的起始位置  
                    StringFormat.GenericDefault);

                // 使用 SolidBrush 填充艺术字
                using (SolidBrush brush = new SolidBrush(Color.Blue))
                {
                    drawingGraphics.FillPath(brush, gp); // 填充艺术字
                }

                // 使用 Pen 绘制艺术字的轮廓
                using (Pen pen = new Pen(Color.Blue, 2))
                {
                    drawingGraphics.DrawPath(pen, gp); // 绘制艺术字的轮廓
                }
            }
        }

        // Panel的Paint事件处理
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            // 将 Bitmap 绘制到 Panel 上
            e.Graphics.DrawImage(drawingBitmap, 0, 0);
        }
    }
}

五.运行结果

(1)点击button1

(2)在textbox中输入你想展现的内容,再点击button2

六.总结

在本次实验中,我们学习了如何使用 C# 和 WinForms 进行图形图像编程。我们首先学习了如何创建一个基本的 Windows 窗体应用程序,并在窗体上添加控件。接着,我们探讨了如何使用 Graphics 对象来进行绘图操作,包括绘制基本图形和处理文本输出。

在实验中,我们实现了以下功能:

  1. 绘制基本图形:我们学会了如何使用 Graphics 对象在面板上绘制矩形和椭圆,并且掌握了如何使用画笔来设置绘图属性。
  2. 添加艺术字:我们学会了如何在面板上绘制艺术字,包括设置字体、测量文本尺寸以及使用渐变色来绘制文本。

通过本次实验,我对 C# 中的图形图像编程有了更深入的了解,掌握了一些常用的绘图技巧和方法。这将对我的软件开发能力产生积极的影响,使我能够更好地处理与图形图像相关的任务和项目。

七.重难点

使用panel为画板,完成以下设计内容:

  1. 使用pen绘制基础图形;
  2. 使用LinearGradientBrush实现渐变色字体;
  3. 使用GraphicsPath实现艺术字,部分核心代码如下所示;

GraphicsPath gp = new GraphicsPath(FillMode.Winding);

            gp.AddString(

                "字体轮廓",new FontFamily("方正舒体"),(int)FontStyle.Regular,

                80,new PointF(10, 20),new StringFormat());

            Brush brush = XXXXXXXXXXXXXXXXXXXXXX;

            XXXDrawPath(Pens.Green, gp);

            XXX.FillPath(brush, gp);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值