如何以编程方式衡量ASP.NET串像素宽度? [英] How to programmatically measure string pixel width in ASP.NET?

查看:146
本文介绍了如何以编程方式衡量ASP.NET串像素宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何得到一个字符串的大小?在Windows窗体很容易,我只是使用图形对象,然后MeasureString功能。在ASP.NET我不知道如何做到这一点。

How do you get the size of a string? In Windows Forms it's easy, I just use graphics object and then MeasureString function. In ASP.NET I'm not sure how to do this.

推荐答案

和汤姆一样Gullen在说什么。你可以创建一个位图和messure字符串。我有这个code我用它来查找像素宽/长。只是改变了字体和大小。

Like Tom Gullen is saying. You can just create a bitmap and messure the string. I have this code I use to find the width/length in pixel. Just change the font and size.

// Bitmap class namespace:
using System.Drawing;

...

private float GetWidthOfString(string str)
{
    Bitmap objBitmap = default(Bitmap);
    Graphics objGraphics = default(Graphics);

    objBitmap = new Bitmap(500, 200);
    objGraphics = Graphics.FromImage(objBitmap);

    SizeF stringSize = objGraphics.MeasureString(str, new Font("Arial", 12));

    objBitmap.Dispose();
    objGraphics.Dispose();
    return stringSize.Width;
}

只是想表明一个例子。

Just wanted to show an example.

这篇关于如何以编程方式衡量ASP.NET串像素宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆