C# Graphics 文本 精确测量与绘制方案 MeasureString替代方案

发布时间:2025-12-09 11:58:36 浏览次数:1

转载请标注原文地址:https://www.cnblogs.com/nanyunan/p/13546143.html

一、测量方案

        public static SizeF MeasureString(string Text,Font f,StringFormat sf)        {            GraphicsPath path = new GraphicsPath();            path.AddString(Text,f.FontFamily,(int)f.Style,f.Size,new PointF(0,0),sf);            return path.GetBounds().Size;        }        public static SizeF MeasureString(string Text, Font f)        {            GraphicsPath path = new GraphicsPath();            path.AddString(Text, f.FontFamily, (int)f.Style, f.Size, new PointF(0, 0), new StringFormat());            return path.GetBounds().Size;        }        public static SizeF MeasureString(string Text, Font f, StringFormat sf,Rectangle rect)        {            GraphicsPath path = new GraphicsPath();            path.AddString(Text, f.FontFamily, (int)f.Style, f.Size, rect, sf);            return path.GetBounds().Size;        }        public static SizeF MeasureString(string Text, Font f, StringFormat sf, int Width)        {            GraphicsPath path = new GraphicsPath();            path.AddString(Text, f.FontFamily, (int)f.Style, f.Size, new Rectangle(0, 0, Width, int.MaxValue), sf);            return path.GetBounds().Size;        }        public static SizeF MeasureString(string Text, Font f,int Width)        {            GraphicsPath path = new GraphicsPath();            path.AddString(Text, f.FontFamily, (int)f.Style, f.Size, new Rectangle(0, 0, Width, int.MaxValue), new StringFormat());            return path.GetBounds().Size;        }

二、计算填充空间的字体大小

        /// <summary>        /// 自适应字体大小        /// </summary>        /// <param name="Text"></param>        /// <param name="Width"></param>        /// <param name="Height"></param>        /// <param name="f"></param>        /// <returns></returns>        public static Font GetFont(string Text,float Width,float Height,Font f)        {            Font tmp = new Font(f.FontFamily, f.Size, f.Style, GraphicsUnit.Point);            int count = 0;            while (true)            {                var size = MeasureString(Text, tmp, (int)Width);                var rw = Width / size.Width;                var rh = Height / size.Height;                var rmin = rw > rh ? rh : rw;                if (Math.Abs(rmin - 1) < 0.01 || (count++)>10)                {                    return new Font(tmp.FontFamily, tmp.Size, tmp.Style, GraphicsUnit.Point);                }                var t = tmp;                tmp = new Font(tmp.FontFamily, rmin*tmp.Size, tmp.Style, GraphicsUnit.Point);                t.Dispose();            }                                }

三、精确绘制

        public void Draw(Bitmap Canvas,string Text,Font f,int x,int y)        {            Graphics g = Graphics.FromImage(Canvas);            StringFormat sf = new StringFormat();            GraphicsPath Path = new GraphicsPath();            Path.AddString(Text, f.FontFamily, (int)f.Style, f.Size, new PointF(0, 0), sf);            var PathBounds = Path.GetBounds();//计算偏移            var dx = x - PathBounds.X;            var dy = y - PathBounds.Y;            Path = new GraphicsPath();//重置            Path.AddString(Text, f.FontFamily, (int)f.Style, f.Size, new PointF(dx,dy), sf);            g.FillPath(Brushes.Black, Path);            g.Dispose();        }
measurestring
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477