Get size of text in pixel C#

21 Mar 2018

Share to:

วิธีหาขนาดของข้อความ ว่ามีขนาดความกว้าง และความสูงกี่ Pixel ทําได้ด้วยตัวอย่าง Function นี้

private SizeF getsizeFont(string text, Font drawFont) {
    Bitmap textBitmap = new Bitmap(1, 1);
    Graphics drawGraphics = Graphics.FromImage(textBitmap);

    return drawGraphics.MeasureString(text, drawFont);
}

วิธีใช้ function คือ

//Width size
float w = getsizeFont("", new Font("Arial", 16)).Width;
//Height size
float h = getsizeFont("", new Font("Arial", 16)).Height;

Copyright © 2019 - 2024 thiti.dev |  v1.19.0 |  Privacy policy |  status | 

            วงแหวนเว็บ