winforms icon indicating copy to clipboard operation
winforms copied to clipboard

PictureBox with stretched image in .Net 8 is 10 times slower than in .Net Framework

Open Camel-RD opened this issue 2 months ago • 8 comments

.NET version

8

Did it work in .NET Framework?

Yes

Did it work in any of the earlier releases of .NET Core or .NET 5+?

No response

Issue description

Drawing of PictureBox with stretched image in .Net 8 is 10 times slower than in .Net Framework. This slowdown is noticeable when form and PictureBox is resized by dragging border of the form with mouse.

Steps to reproduce

I have a simple form with a PictureBox that is larger than the image assigned to it and with SizeMode set to PictureBoxSizeMode.StretchImage.

Im using this code to test drawing performance of this form.

Stopwatch sw = new Stopwatch();
long ts = 0;
long ct = 1000;

sw.Restart();
for (int i = 0; i < ct; i++)
{
	Refresh();
}
sw.Stop();

ts += sw.ElapsedMilliseconds;
decimal m = 1M * ts / ct;
var msg = $"count:{ct} time(milliseconds):{ts} average:{m:0.000}";
MessageBox.Show(msg);

In .Net it takes 10 times more time to do this than in .Net Framework.

TestPictureBoxSpeed.zip

Camel-RD avatar Oct 31 '25 15:10 Camel-RD

@JeremyKuhne - could this be System.Drawing related? @LeafShi1 - please investigate if you can find where a regression may have occurred. Perhaps test with .NET 6, 8, and 9.

merriemcgaw avatar Oct 31 '25 16:10 merriemcgaw

could this be System.Drawing related?

@merriemcgaw Possibly, although I'd be more suspicious of other layout impacting changes. @LeafShi1 also worth comparing performance in .NET 10.

JeremyKuhne avatar Oct 31 '25 18:10 JeremyKuhne

I have noticed that this slowdown happens in x64 build, in x86 build Picturebox is 5 to 6 times faster.

Camel-RD avatar Oct 31 '25 19:10 Camel-RD

  • AnyCPU Behavior Differs Between Frameworks
    • In .NET Framework, AnyCPU typically defaults to x86 when “Prefer 32-bit” is enabled.
    • In .NET Core / .NET 5+, AnyCPU defaults to x64 because the host process is 64-bit.
    • Therefore, performance results under AnyCPU are not directly comparable across these frameworks.
  • x64 Performance Trend
    • Starting from .NET 5.0, x64 performance becomes stable, averaging around 10 ms.
    • Compared to .NET Framework x64 (≈42 ms), this is a significant improvement.
  • x86 Performance Trend
    • On .NET 5.0, x86 performance is noticeably worse (≈10.7 ms) than .NET Framework (≈4.5 ms).
    • Later versions show gradual improvement, but still remain slower than .NET Framework.
Image

This difference occurs between .NET Core 3.1 and .NET 5.0.

LeafShi1 avatar Nov 03 '25 03:11 LeafShi1

For a maximized form (FullHD) with a docked PictureBox inside it im getting these numbers:

  • application built for X86 - 11ms;
  • application built for X64 - 71ms;

Camel-RD avatar Nov 03 '25 07:11 Camel-RD

It's true that larger images take longer, but the difference between x86 and x64 isn't as significant as six or seven times.

.NET 10.0 project, maximized form with a docked PictureBox

On 2560x1600 screen:

  • x86 - 24ms
  • x64 - 38ms

On 3840x2160 screen:

  • x86 - 51ms
  • x64 - 77ms

LeafShi1 avatar Nov 03 '25 08:11 LeafShi1

I did the testing for .Net 8 on 2 PC - Win10 and Win11 (and on VM Win10) and in all cases i saw the performance 7 times lower on x64 build.

Camel-RD avatar Nov 03 '25 08:11 Camel-RD

Another thing worries me: why is x64 so much slower than x86 everywhere?

kirsan31 avatar Nov 06 '25 17:11 kirsan31