Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

I have a Windows form application.
I draw some string with DrawString function on this form in OnPaint event with Font settings, which is set in Form properties.

I put a Label component with the same text and the same Font as the Form Font and the text have a little bit different appearance, especially, when the font size are bigger.

With default font size 8.25 these strings look same.

Can somebody tell me, why they look different when is using the same font settings?

Thank you for reply.

Peter

[EDIT]

Thank you for reply.

Here is my code of the Paint event:

Collapse

C#
private void Form1_Paint(object sender, PaintEventArgs e)
{
    string str = "abcdefgh"
 
    SolidBrush brush = new SolidBrush(Color.Red);
 
    e.Graphics.DrawString(str, this.Font, brush, new PointF(100.0f, 100.0f),
                StringFormat.GenericTypographic);
}

And the Label component has the same Font setting as the Form. But the strings look a little bit different.
Posted
Updated 4-May-11 9:45am
v5
Comments
Dalek Dave 4-May-11 3:54am    
Edited for Readability.

Probably this is because you System.Drawing.Graphics.DrawString relies on the font passed as a parameter, not the font of the Form. By this reason, I don't really know which font did you use for rendering of your string via the event Paint. (The name of event is Paint, not OnPaint which is the name of the virtual method which you can override, alternatively.)

If this clue did not help you to resolve this confusion yet, you can post few lines of code: the lines when the fonts involved are created, assigned to a property or passed as a parameter, so include the line with DrawString. Please use "Improve question" if you want to add this code.

—SA
 
Share this answer
 
v2
Comments
Olivier Levrey 4-May-11 8:00am    
OP added answers instead of comments. Please see.
Sergey Alexandrovich Kryukov 4-May-11 15:44pm    
OP Commented:

I found out some behaviour.
When I changed font size and font unit is set to point, strings are equal at some sizes. When I change font unit to pixel, strings are still equal. There is no difference between them.
Maybe, the problem is how DrawString calculate some font size.
Sergey Alexandrovich Kryukov 4-May-11 15:49pm    
From this code I now see you're using the Form font. Now, how do you know that Form font and label font are the same. Yes, if unit is different the fonts are different but is fonts are identical, the rendered text should look exactly the same. Also, the label can inherit the parent's font.
--SA
[Moved to comment to the solution by SA]
Use "Improve question", "Add comment" or reply to comment.
 
Share this answer
 
v2
The label control (and other controls I assume) are drawn using various text renderings depending on system settings.
This is the OnPaint method of the Label which shows what's going on. You could emulate the relevant parts in your own code. To go deeper you will need a tool such as Reflector to dig into the .NET source.
C#
protected override void OnPaint(PaintEventArgs e)
{
    Color nearestColor;
    this.Animate();
    Rectangle r = LayoutUtils.DeflateRect(base.ClientRectangle, base.Padding);
    ImageAnimator.UpdateFrames();
    Image image = this.Image;
    if (image != null)
    {
        this.DrawImage(e.Graphics, image, r, base.RtlTranslateAlignment(this.ImageAlign));
    }
    IntPtr hdc = e.Graphics.GetHdc();
    try
    {
        using (WindowsGraphics graphics = WindowsGraphics.FromHdc(hdc))
        {
            nearestColor = graphics.GetNearestColor(base.Enabled ? this.ForeColor : base.DisabledColor);
        }
    }
    finally
    {
        e.Graphics.ReleaseHdc();
    }
    if (this.AutoEllipsis)
    {
        Rectangle clientRectangle = base.ClientRectangle;
        Size preferredSize = this.GetPreferredSize(new Size(clientRectangle.Width, clientRectangle.Height));
        this.showToolTip = (clientRectangle.Width < preferredSize.Width) || (clientRectangle.Height < preferredSize.Height);
    }
    else
    {
        this.showToolTip = false;
    }
    if (this.UseCompatibleTextRendering)
    {
        using (StringFormat format = this.CreateStringFormat())
        {
            if (base.Enabled)
            {
                using (Brush brush = new SolidBrush(nearestColor))
                {
                    e.Graphics.DrawString(this.Text, this.Font, brush, r, format);
                    goto Label_01BF;
                }
            }
            ControlPaint.DrawStringDisabled(e.Graphics, this.Text, this.Font, nearestColor, r, format);
            goto Label_01BF;
        }
    }
    TextFormatFlags flags = this.CreateTextFormatFlags();
    if (base.Enabled)
    {
        TextRenderer.DrawText(e.Graphics, this.Text, this.Font, r, nearestColor, flags);
    }
    else
    {
        Color foreColor = TextRenderer.DisabledTextColor(this.BackColor);
        TextRenderer.DrawText(e.Graphics, this.Text, this.Font, r, foreColor, flags);
    }
Label_01BF:
    base.OnPaint(e);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900