星期二, 1月 05, 2021

[C#] 繪製文字 - 測量

閱讀該篇 Graphics.MeasureString 方法 來了解如何測量文字,文章內的重點備註
The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the string to allow for overhanging glyphs. Also, the DrawString method adjusts glyph points to optimize display quality and might display a string narrower than reported by MeasureString.
找到該篇 GDI+ Text, Resolution Independence, and Rendering Methods. Or - Why does my text look different in GDI+ and in GDI? 來閱讀,才了解上述文字要表達的意思
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;

namespace GraphicsSample
{
    public partial class FrmMeasureString : Form
    {
        public FrmMeasureString()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            MeasureStringMin(e);
        }

        private void MeasureStringMin(PaintEventArgs e)
        {
            string measureString = "文字寬度測量";
            Font stringFont = new Font("微軟正黑體", 16);

            // Figure1:MeasureString 前後會留些許空白
            SizeF stringSize = e.Graphics.MeasureString(measureString, stringFont);
            float stringSizeX = 80.0F;
            float stringSizeY = 10.0F;
            e.Graphics.DrawRectangle(Pens.Red, stringSizeX, stringSizeY, stringSize.Width, stringSize.Height);
            e.Graphics.DrawString(measureString, stringFont, Brushes.Black, stringSizeX, stringSizeY);

            // Figure2:MeasureString 搭配 StringFormat.GenericTypographic
            SizeF stringSizeFormat = e.Graphics.MeasureString(measureString, stringFont , int.MaxValue , StringFormat.GenericTypographic);
            float stringSizeFormatX = 80.0F;
            float stringSizeFormatY = 50.0F;
            e.Graphics.DrawRectangle(Pens.Red, stringSizeFormatX, stringSizeFormatY, stringSizeFormat.Width, stringSizeFormat.Height);
            e.Graphics.DrawString(measureString, stringFont, Brushes.Black, stringSizeFormatX , stringSizeFormatY);

            // Figure3:MeasureString 搭配 StringFormat.GenericTypographic + TextRenderingHint.AntiAlias
            e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
            SizeF stringSizeAntiAlias = e.Graphics.MeasureString(measureString, stringFont, int.MaxValue, StringFormat.GenericTypographic);
            float stringSizeAntiAliasX = 80.0F;
            float stringSizeAntiAliasY = 90.0F;
            e.Graphics.DrawRectangle(Pens.Red, stringSizeAntiAliasX, stringSizeAntiAliasY, stringSizeAntiAlias.Width, stringSizeAntiAlias.Height);
            e.Graphics.DrawString(measureString, stringFont, Brushes.Black, stringSizeAntiAliasX, stringSizeAntiAliasY);

            // Figure4:MeasureString 搭配 StringFormat.GenericTypographic + TextRenderingHint.AntiAliasGridFit
            e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
            SizeF stringSizeAntiFit = e.Graphics.MeasureString(measureString, stringFont, int.MaxValue, StringFormat.GenericTypographic);
            float stringSizeAntiFitX = 80.0F;
            float stringSizeAntiFitY = 130.0F;
            e.Graphics.DrawRectangle(Pens.Red, stringSizeAntiFitX, stringSizeAntiFitY, stringSizeAntiFit.Width, stringSizeAntiFit.Height);
            e.Graphics.DrawString(measureString, stringFont, Brushes.Black, stringSizeAntiFitX, stringSizeAntiFitY);
        }
    }
}

沒有留言:

張貼留言