程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> MeasureString 通過文本寬度獲取繪制高度

MeasureString 通過文本寬度獲取繪制高度

編輯:C#入門知識

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;


namespace NewsWebApp.Biz
{
   
 
    public  class TextCount
    {
        System.Drawing.Font gdiFont;
        System.Drawing.Graphics graphics;
        public Font Font
        {
            get { return this.font; }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");
                if (this.font != value)
                {
                    this.font = value;
                    this.gdiFont = null;
                }
            }
        }
        Font font;
 
       /// <summary>
       /// 通過寬度測量文本高度
       /// </summary>
       /// <param name="text">文本</param>
       /// <param name="Textwidth">文本高度</param>
       /// <returns></returns>
        public  float GetHeightByText(string text, int Textwidth)
        {
            Graphics g = Realize();
            Font stringFont = new Font("Arial", 16);
            StringFormat newStringFormat = new StringFormat();
            newStringFormat.FormatFlags = StringFormatFlags.LineLimit;
            // Measure string.
            SizeF stringSize = new SizeF();
            stringSize = g.MeasureString(text, stringFont, Textwidth, newStringFormat);
            return stringSize.Height;
        }

        Graphics Realize()
        {
            if (this.graphics == null)
                this.graphics = Graphics.FromHwnd(IntPtr.Zero);
            this.graphics.PageUnit = GraphicsUnit.Point;
           
            return this.graphics;
        }
 

    }
}
 

      

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved