C# Graphics绘制文本常用方法源码

using System;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DGIString
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InstalledFontCollection col = new InstalledFontCollection();

            foreach (var item in col.Families)
            {
                listBox1.Items.Add(item.Name);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics G = e.Graphics;

            G.TextRenderingHint = TextRenderingHint.AntiAlias;

            FontFamily fontFamily = new FontFamily("楷体");

            Font font1 = new Font(fontFamily, 20f, FontStyle.Bold, GraphicsUnit.Pixel);
            string poem1 = "床前明月光,";
            G.DrawString(poem1, font1, Brushes.Black, 0, 10);

            Font font2 = new Font("宋体", 20f, FontStyle.Italic, GraphicsUnit.Pixel);
            string poem2 = "疑是地上霜。";
            SizeF size2 = G.MeasureString(poem2, font2);
            RectangleF rect2 = new RectangleF(0, 40, size2.Width, size2.Height);
            G.DrawRectangle(Pens.Red, rect2.X, rect2.Y, rect2.Width, rect2.Height);
            G.DrawString(poem2, font2, Brushes.Red, rect2);

            Font font3 = new Font("黑体", 20f, FontStyle.Strikeout|FontStyle.Italic, GraphicsUnit.Pixel);
            Rectangle rect3 = new Rectangle(0, 70, 150, 50);
            StringFormat sf3 = new StringFormat();
            sf3.Alignment = StringAlignment.Center;
            sf3.LineAlignment = StringAlignment.Center;
            string poem3 = "举头望明月,";
            G.DrawRectangle(Pens.Green, rect3);
            G.DrawString(poem3, font3, Brushes.Green, rect3, sf3);

            Font font4 = new Font("幼圆", 20f, FontStyle.Underline|FontStyle.Bold, GraphicsUnit.Pixel);
            StringFormat sf4 = new StringFormat(StringFormatFlags.DirectionVertical | StringFormatFlags.DirectionRightToLeft);
            Rectangle rect4 = new Rectangle(0, 120, 50, 100);
            string poem4 = "低头思故乡。";
            G.DrawRectangle(Pens.Blue, rect4);
            G.DrawString(poem4, font4, Brushes.Blue, rect4, sf4);

            string fontName = "宋体";
            if (listBox1.SelectedItem != null && !string.IsNullOrEmpty(listBox1.SelectedItem.ToString()))
            {
                fontName = listBox1.SelectedItem.ToString();
            }

            Font font = new Font(fontName, 25f);
            StringBuilder poem = new StringBuilder();
            poem.Append(poem1).Append(poem2).Append(poem3).Append(poem4);

            HatchBrush hatachBrush = new HatchBrush(HatchStyle.Cross, Color.Lime);
            Rectangle rect = new Rectangle(150, 0, 150, 260);
            G.DrawRectangle(Pens.Lime, rect);
            G.DrawString(poem.ToString(), font, hatachBrush, rect);
        }
    }
}
 

你可能感兴趣的:(C#)