Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange FontSize change

I've encountered a strange thing today, working with winforms.

I've created a RichTextBox with default FontSize of 14.25 pt.

I was performing some operation and I needed to create temporary RichTextBox to which I have copied selected text fromy my original RichtextBox. After that, without no changes made by me, the whole tempRichTextBox text's font size increased exactly 0.25 pt. Every single letter. Is that some bug or what?

using (RichTextBox tempRichTextBox = new RichTextBox())
{
    tempRichTextBox.Rtf = this.richTextBox.SelectedRtf;
    int tempStart = this.richTextBox.SelectionStart;
    int tempLength = this.richTextBox.SelectionLength;
    for (int i = 0; i < tempLength; i++)
    {
        tempRichTextBox.Select(i, 1);
        this.baseSize = tempRichTextBox.SelectionFont.Size;
    }
    tempRichTextBox.Select(0, tempLength);
    this.richTextBox.SelectedRtf = tempRichTextBox.SelectedRtf;
    this.richTextBox.Select(tempStart, tempLength);
}

Does anybody has an idea why is that happening?

like image 241
Paweł Poręba Avatar asked Oct 15 '25 15:10

Paweł Poręba


1 Answers

I found that WinForms adds to font size (or sometimes subtracts from it) 0.25. You see 14.25 in designer, but it is achieved because value 14 was initially stored there. I saw that happening also when playing with FontDialog system dialog alone. You choose font size 8 and in returned Font object you find 8.25. On some sizes I have found things like 14.75 instead of 15. But it is not growing with font size, i.e. you will find the same small differences with font size = 5000.

Cause: font size is changing in steps of 0.75. It is related to DPI and font size units.

So implement formula taking that into account and you should start getting more expectable results. Just note that DPI or font size units can be different in context of FontDialog and RTB.

like image 154
miroxlav Avatar answered Oct 18 '25 09:10

miroxlav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!