Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
741 views
in Technique[技术] by (71.8m points)

c# - How to make text/labels smooth?

Does anyone know how to make labels, or text, smoother? At the moment they look quite jagged. As I want to make the label dynamic, I can't just insert the text from Photoshop.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You'll have to dynamically generate images representing your text if you want to anti-alias it. Here is an example on msdn: http://msdn.microsoft.com/en-us/library/a619zh6z.aspx

EDIT: Editing per comment below.

The link describes using the OnPaint event of your control to use a different TextRenderingHint. If you're wanting something a little more re-useable what you can do is create a Custom Label class that extends the Label class, and use this in your forms:

public partial class CustomLabel : Label
{
    private TextRenderingHint _hint = TextRenderingHint.SystemDefault;   
    public TextRenderingHint TextRenderingHint
    {
        get { return this._hint; }
        set { this._hint = value; }
    }        

    protected override void OnPaint(PaintEventArgs pe)
    {            
        pe.Graphics.TextRenderingHint = TextRenderingHint;
        base.OnPaint(pe);
    }
}

Add a new Custom Control called CustomLabel (or whatever you want to call it) and use the code above. Rebuild your project and you should then see the CustomLabel control appear in your toolbox at the top, under the "MyProject Components" category. In the properties pane for this custom label you'll see the new TextRenderingHint property. Set this to "AntiAlias." Add another label to your form and compare how they look.

If you want to default it to AntiAlias simply change the default value of the private variable.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...