如何测量宽度的性格precisely? [英] How to measure width of character precisely?

查看:172
本文介绍了如何测量宽度的性格precisely?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我有一些错误,但是......我想模拟字符间距。 我破字(文)的单字符列表,测量它们的宽度,然后他们画了一个又一个的位图。我认为,这呈现的文本的总宽度是相同的,整个并不是分裂字符串的宽度,但也有一些是错误的。在一个循环中呈现的字符显示更宽的结果。有没有什么办法让普通(预期)的结果?

Maybe I've got something wrong, but... I want to simulate character spacing. I break the word (text) into the list of single characters, measure their widths, and then painting them one after another on the bitmap. I supposed, that overall width of the rendered text will be the same as the width of the whole not splitted string, but there is something wrong. Rendering characters in a loop show wider result. Is there any way to get common (expected) results?

下面是一个code片断:

here is a code snippet:

private struct CharWidths
{
    public char Char;
    public float Width;
}

private List<CharWidths> CharacterWidths = new List<CharWidths>();

...

private void GetCharacterWidths(string Text, Bitmap BMP)
{
    int i;
    int l = Text.Length;
    CharacterWidths.Clear();
    Graphics g = Graphics.FromImage(BMP);

    CharWidths cw = new CharWidths();
    for (i = 0; i < l; i++)
    {
        Size textSize = TextRenderer.MeasureText(Text[i].ToString(), Font);
        cw.Char = Text[i];
        cw.Width = textSize.Width;
        CharacterWidths.Add(cw);
    }
}

...

public void RenderToBitmap(Bitmap BMP)
{
    //MessageBox.Show("color");

    Graphics g = Graphics.FromImage(BMP);
    GetCharacterWidths("Lyborko", BMP);

    int i;
    float X = 0;
    PointF P = new PointF(); 
    for (i = 0; i < CharacterWidths.Count; i++)
    {
        P.X = X;
        P.Y = 0;

        g.DrawString(CharacterWidths[i].Char.ToString(), Font, Brushes.White, P);

        X = X+CharacterWidths[i].Width;
    }

    P.X = 0;
    P.Y = 30;
    g.DrawString("Lyborko", Font, Brushes.White, P);
    // see the difference
}

感谢名单了很多

Thanx a lot

推荐答案

首先应该说,没有这一个一劳永逸的解决方案,但对问题的夫妇suggessions的:

First of all should say that don't have a silver bullet solution for this, but have a couple of suggessions on subject:

  1. 考虑到通过调用 TextRenderer.MeasureText 不通过电流设备上下文(同​​一个你用来绘制后的字符串),知道一个简单的事实, MeasureText 简单的情况下缺乏该参数的创建一个新的使用台式机和电话 DrawTextEx WindowsSDK功能,我会说第一次使用的 MeasureText ,您可以指定喜欢第一个参数设备上下文您使用后呈现的文本。 的有所作为。

  1. Considering that you by calling TextRenderer.MeasureText do not pass current device context (the same one you use to draw a string after) and knowing a simple fact that MeasureText simply in case of lack of that parameter creates a new one compatible with desktop and calls DrawTextEx WindowsSDK function, I would say first use an overload of MeasureText where you specify like a first argument device context which you use to render a text after. Could make a difference.

如果失败,我会的尝试的使用的 Control.Get preferredSize 以方法的猜到的最presize可能呈现在屏幕上的控制层面,这样的实际上的你未来的字符串的位图的尺寸。要做到这一点,你可以创建一些临时的控制,分配字符串,渲染和调用这个函数之后。很明显,我认为的这个的解决方案可能很难适应你的应用程序体系结构,但是的可以的可能产生较好的效果。

If it fails, I would try to use Control.GetPreferredSize method to guess most presize possible rendering dimension of the control on the screen, so actually the dimension of you future string's bitmap. To do that you can create some temporary control, assign a string, render and after call this function. It's clear to me that this solution may hardly fit in your app architecture, but can possibly produce a better results.

希望这有助于。

这篇关于如何测量宽度的性格precisely?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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