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

This Wrox Blox teaches you how to add graphics to C# 2008 applications, explaining fundamental graphics techniques such as: drawing shapes with different colors and line styles; filling areas with colors, gradients, and patterns; drawing text that is properly aligned, sized, and clipped exactly where you want it; manipulating images and saving results in bitmap, JPEG, and other types of files. Also covered are instructions for how to greatly increase your graphics capabilities using transformations. Transformations allow you to move, stretch, or rotate graphics. They also let you work in coordinate systems that make sense for your application. You will also learn how to use all of these techniques in printouts. The author describes the sequence of events that produce a printout and shows how to generate and preview printouts. The final sections describe two powerful new graphic tools that were introduced with .NET Framework 3.0: WPF graphics and FlowDocuments. WPF applications can use XAML graphic commands to declaratively draw and fill the same kinds of shapes that a program can draw by using graphics objects. Finally, a discussion on the FlowDocument object shows you how to define items that should be flowed across multiple pages as space permits. This lets you display text, graphics, controls, and other items that automatically flow across page breaks. FlowDocument viewers make displaying these documents easy for you, and simplifies the user's reading of the documents. This Wrox Blox also contains 35 example programs written in C# 2008, although most of the code works in previous versions of C# as well. The most notable exceptions are WPF graphics and FlowDocuments, both of which require WPF provided in .NET Framework 3.0 and later.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值