黃聰:C#如何通過MeasureString、Graphics擷取字串的像素長度

來源:互聯網
上載者:User

標籤:style   blog   ar   color   os   使用   sp   for   strong   

1、    使用g.MeasureString()獲得

使用MeasureString測量出來的字元寬度,總是比實際寬度大一些,而且隨著字元的長度增大,貌似實際寬度和測量寬度的差距也越來越大了。查了一下MSDN,找到了下面這個理由:MeasureString 方法旨在與個別字串一起使用,它在字串前後包括少量額外的空格供突出的標誌符號使用。

string str;str = "大";Font f = new Font("SimSun", 7F, System.Drawing.FontStyle.Regular);Graphics g = this.CreateGraphics();//單位為mmg.PageUnit = GraphicsUnit.Millimeter;SizeF sim = g.MeasureString(str, f);

 

2、使用TextRenderer.MeasureText獲得,提供使用指定尺寸建立文本初始邊框時,使用指定的裝置上下文、字型和格式說明所繪製的指定文本的尺寸(以像素為單位)。

private void MeasureText(PaintEventArgs e) {            string str;            str = "大家好";            Font f = new Font("SimSun", 7F, System.Drawing.FontStyle.Regular);            Size sif = TextRenderer.MeasureText(e.Graphics, str, f, new Size(0, 0), TextFormatFlags.NoPadding);            MessageBox.Show((sif.Width / pdi).ToString());}private void print(object sender, PaintEventArgs e){            MeasureText(e);}

 

黃聰:C#如何通過MeasureString、Graphics擷取字串的像素長度

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.