Problem with form sizes (scaling) after migrating WinForms application from .NET Framework to .NET 6 or .NET 8

We are in the process of migrating a fairly large WinForms code base from .NET Framework 4.8 to .NET 6 (or .NET 8, same issue there).

Most things seem to work. However, there is one visually obvious problem I struggled to find a solution for.

The problem is that the sizes of forms are not correct at runtime. Every form is a little bit larger at runtime than at design time.

I managed to track the problem down to the AutoScaleDimensions property.

  • The old WinForms designer sets this property to the value (6, 13), either in the generated designer code or in a resource file (depending on whether the form is localizable).

  • After the form is opened, the AutoScaleDimensions property has the value (7, 15).

Inside the generated designer code, there is a call to ResumeLayout(). This call seems to rescale the form size according to the difference between the two values of AutoScaleDimensions (so by a factor of 7/6 = 1.1666 for the width, and 15/13 = 1.1538 for the height).

Below you find the values of certain properties when stepping through the designer code:

private void InitializeComponent()
{
    components = new System.ComponentModel.Container();
    this.SuspendLayout();
    // Here, Size = 300x300, AutoScaleDimensions = 0x0
    this.AutoScaleDimensions = new System.Drawing.SizeF(6.0!, 13.0!); 
    // Now, Size = 300x300, AutoScaleDimensions = 6x13
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    // Now, Size = 300x300, AutoScaleDimensions = 6x13
    this.ClientSize = new System.Drawing.Size(500, 500);
    // Now, Size = 516x539, ClientSize = 500x500, AutoScaleDimensions = 6x13
    this.ResumeLayout(false);
    // Now, Size = 599x616, ClientSize = 583x577, AutoScaleDimensions = 7x15
}

When I remove the SuspendLayout/ResumeLayout calls, it works as expected (the ClientSize stays at 500×500). However, this might have other unintended consequences. I assume there is a reason why the designer put them there.

I looked into the disassembly of the CurrentAutoScaleDimensions property and noticed that it depends on the font size (due to AutoScaleMode.Font).

It appears that the default font was changed in .NET 6 to be Segoe UI at size 9pt. And that’s where the AutoScaleDimensions gets its value from. The solution appears to be to change the default font for the application back to the old one, like so:

System.Windows.Forms.Application.SetDefaultFont(new Font(new FontFamily("Microsoft Sans Serif"), 8.25F))

The solution is to change the default font for the application back to the old one, by adding the following line to your startup method:

System.Windows.Forms.Application.SetDefaultFont(new Font(new FontFamily("Microsoft Sans Serif"), 8.25F))

The important bit is the font size. The font can be Segoe UI if you prefer, it will still work.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Problem with form sizes (scaling) after migrating WinForms application from .NET Framework to .NET 6 or .NET 8

We are in the process of migrating a fairly large WinForms code base from .NET Framework 4.8 to .NET 6 (or .NET 8, same issue there).

Most things seem to work. However, there is one visually obvious problem I struggled to find a solution for.

The problem is that the sizes of forms are not correct at runtime. Every form is a little bit larger at runtime than at design time.

I managed to track the problem down to the AutoScaleDimensions property.

  • The old WinForms designer sets this property to the value (6, 13), either in the generated designer code or in a resource file (depending on whether the form is localizable).

  • After the form is opened, the AutoScaleDimensions property has the value (7, 15).

Inside the generated designer code, there is a call to ResumeLayout(). This call seems to rescale the form size according to the difference between the two values of AutoScaleDimensions (so by a factor of 7/6 = 1.1666 for the width, and 15/13 = 1.1538 for the height).

Below you find the values of certain properties when stepping through the designer code:

private void InitializeComponent()
{
    components = new System.ComponentModel.Container();
    this.SuspendLayout();
    // Here, Size = 300x300, AutoScaleDimensions = 0x0
    this.AutoScaleDimensions = new System.Drawing.SizeF(6.0!, 13.0!); 
    // Now, Size = 300x300, AutoScaleDimensions = 6x13
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    // Now, Size = 300x300, AutoScaleDimensions = 6x13
    this.ClientSize = new System.Drawing.Size(500, 500);
    // Now, Size = 516x539, ClientSize = 500x500, AutoScaleDimensions = 6x13
    this.ResumeLayout(false);
    // Now, Size = 599x616, ClientSize = 583x577, AutoScaleDimensions = 7x15
}

When I remove the SuspendLayout/ResumeLayout calls, it works as expected (the ClientSize stays at 500×500). However, this might have other unintended consequences. I assume there is a reason why the designer put them there.

I looked into the disassembly of the CurrentAutoScaleDimensions property and noticed that it depends on the font size (due to AutoScaleMode.Font).

It appears that the default font was changed in .NET 6 to be Segoe UI at size 9pt. And that’s where the AutoScaleDimensions gets its value from. The solution appears to be to change the default font for the application back to the old one, like so:

System.Windows.Forms.Application.SetDefaultFont(new Font(new FontFamily("Microsoft Sans Serif"), 8.25F))

The solution is to change the default font for the application back to the old one, by adding the following line to your startup method:

System.Windows.Forms.Application.SetDefaultFont(new Font(new FontFamily("Microsoft Sans Serif"), 8.25F))

The important bit is the font size. The font can be Segoe UI if you prefer, it will still work.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật