Anti-Aliasing

kurt69

Well-known member
Joined
Jan 17, 2006
Messages
78
Location
Australia
Programming Experience
Beginner
Hey, I've got (another) problem I've run into. I'm guessing you might see a few questions from me on here. I have searched and tried to do this myself but to no avail, I need some 2nd person guidance.

I have loads of labels on my form which I would like to anti-alias. Some of these labels are used for interactivity (buttons). So how do I go about anti-aliasing the labels already on the form so they can still be used as buttons? Please help I'm lost.
 
yeap :), is that possible?
edit: I still want the same code to execute once i click the label though.

on a side note, i didnt realise that was your sig about increasing winforms load times.
 
Yes it's possible, but it's not as simple as setting a property of the label.
You'll have to subClass the label control and do the drawing yourself. Of course there are many examples of custom drawn labels on the internet.
Creating a custom control will allow you to do many things graphically including text anti-aliasing and even drop shadows if you wish.
Here's a tip: before drawing the string (System.Drawing.Graphics.DrawString) set the SmoothingMode of the graphics object in use to System.Drawing.Drawing2D.SmoothingMode.AntiAlias.
 
Hi, I followed this: http://www.codeproject.com/vb/net/vertical_label.asp and got this;
VB.NET:
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim sngControlWidth As Single
        Dim sngControlHeight As Single
        Dim sngTransformX As Single
        Dim sngTransformY As Single
        Dim labelColor As Color
        Dim labelBorderPen As New Pen(labelColor, 0)
        Dim labelBackColorBrush As New SolidBrush(labelColor)
        Dim labelForeColorBrush As New SolidBrush(MyBase.ForeColor)
        MyBase.OnPaint(e)
        sngControlWidth = Me.Size.Width
        sngControlHeight = Me.Size.Height
        e.Graphics.DrawRectangle(labelBorderPen, 0, 0, sngControlWidth, sngControlHeight)
        e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, sngControlWidth, sngControlHeight)
        ' set the translation point for the 
        ' graphics object - the new (0,0) location
        sngTransformX = 0
        sngTransformY = sngControlHeight
        ' translate the origin used for rotation and drawing 
        e.Graphics.TranslateTransform(sngTransformX, sngTransformY)                             ' (0, textwidth)
        'set the rotation angle for vertical text
        e.Graphics.RotateTransform(180)
        ' draw the text on the control
        e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        e.Graphics.DrawString(labelText, Font, labelForeColorBrush, 0, 0)
    End Sub
    Private Sub VTextBox_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        Invalidate()
    End Sub
    Public Overrides Property Text() As String
        Get
            Return labelText
        End Get
        Set(ByVal Value As String)
            labelText = Value
            Invalidate()
        End Set
    End Property
End Class

How do I build it and add it to my toolbox, then how do I make the already existing labels use that?
 
Last edited:
Actually you dont need to totally create your own control to have text that is antialiased. Just create a new usercontrol change the inherits statement so that it inherits from a label. Then override the onpaint event and simply put the following.

VB.NET:
e.graphics.textrenderinghint = system.drawing.text.textrenderinghint.Cleartypegridfit.
Mybase.onpaint(e)

If it's part of a windows control library then adding it to your toolbox is easy. Build your control library, then go to your project, right click the toolbox and go to add/remove items. Hit browse goto the bin folder of your control library and doubleclick on the DLL and there you go. A label with antialiased text with two lines of code.
 
Nevermind what I wrote, it was a private sub not a protected override thats why it wouldn't work. BTW thanks for your help with this and every question I've asked. My application looks a whole lot better now. :D

I've uploaded my application and I would like some feedback if you's dont mind? I haven't virus scanned it but im 99.99% sure it hasnt been infected by any worms or whatever as I do my best to keep my computer virusfree. Just as a precaution scan it yourself.

Anyway feedback on my UI would be nice, but please note when creating a character that only Bat and Deep will work as I haven't added any others yet. The only 2 modes that will work are Character Menu and a 0.1% finished Training Mode. Thanks.

Link to File: http://www.badongo.com/file/476442
 
Last edited:
Can you give exact example? I need to create antialiased scrolling text on a form. (teleprompter) - for privat use only.

I have skills in forms, labels, text, files like .txt .doc's .xls etc. but not so deep in graphics.
 
Back
Top