0

テキストとフォントの詳細(フォント、サイズなど)をdll呼び出しに渡したい

テキストの幅と高さをピクセル単位で取得したい

クラシック ASP から呼び出されるため、dll に含まれている必要があります。

TextMetrics のようなものがあることは知っていますが、これを COM オブジェクトにラップする方法がわかりません。

これを行うにはどうすればよいですか (C# でお願いします)。

4

3 に答える 3

4

おそらく、Graphics.MeasureString を使用できます。

テキストとフォントを System.Drawing.Font オブジェクトとして渡します。このメソッドは System.Drawing.SizeF オブジェクトを返します。

それが役に立てば幸い。

さよなら!

申し訳ありませんが、編集しました:(わかりました..巨大なもの)

using System;
using System.Drawing;

namespace MeasureSize
{
    class Program
    {
        static void Main(string[] args)
        {
            var size = GetTextSize("This is a test!", "Arial", 10, "normal", "bold");

            Console.Write("Width: {0} / Heigth: {1}", size);
            Console.ReadKey();
        }

        public static object[] GetTextSize(object value, object fontFamily, object size, object style, object weight)
        {
            if (value == null || fontFamily == null || size == null) { return new object[0]; }

            var result = new object[2];
            var text = value.ToString();
            var font = default(Font);
            var composedStyle = string.Concat(style ?? "normal", "+", weight ?? "normal").ToLowerInvariant();
            var fontStyle = default(FontStyle);

            switch (composedStyle)
            {
                case "normal+normal": fontStyle = FontStyle.Regular | FontStyle.Regular; break;
                case "normal+bold": fontStyle = FontStyle.Regular | FontStyle.Bold; break;
                case "italic+normal": fontStyle = FontStyle.Italic | FontStyle.Regular; break;
                case "italic+bold": fontStyle = FontStyle.Italic | FontStyle.Bold; break;
            }

            font = new Font(fontFamily.ToString(), Convert.ToSingle(size), fontStyle, GraphicsUnit.Pixel);

            using (var image = new Bitmap(1, 1))
            using (var graphics = Graphics.FromImage(image))
            {
                var sizeF = graphics.MeasureString(text, font);

                result[0] = Math.Round((decimal)sizeF.Width, 0, MidpointRounding.ToEven);
                result[1] = Math.Round((decimal)sizeF.Height, 0, MidpointRounding.ToEven);
            }

            return result;
        }
    }
}
于 2012-08-01T12:54:06.423 に答える
1

そのようなものかもしれません(ASPで動作します)

public static SizeF MeasureString(string s, Font font)
{
    SizeF result;
    using (var image = new Bitmap(1, 1))
    {
        using (var g = Graphics.FromImage(image))
        {
            result = g.MeasureString(s, font);
        }
    }

    return result;
}
于 2012-08-01T12:55:21.367 に答える
0

これらの msdn リンクは、探しているものを提供する必要があり ます.aspx

于 2012-08-01T13:03:43.153 に答える