-
Notifications
You must be signed in to change notification settings - Fork 938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resizing System.Drawing.Bitmap to less than 1/128th (Bilinear) or 1/256th (Bicubic) of its original width/height corrupts the result #11248
Comments
@JeremyKuhne is probably best to look at System.Drawing issues like this. |
Unfortunately, we're unlikely to get a fix for GDI+. If we find a crash or AV here that would be different. For now, it's good to have this written up here. This resizing pattern is pretty common I think. We might want to consider adding a If we were to add such an API we would want to do a thorough check on the conditions of the failure here. Does this happen for all source image formats? Is it related to a particular source image format (png, jpg, etc.)? |
I suspect with images becoming significantly larger this issue will become more common. I am not sure throwing is the correct response because that would break existing applications. I am not sure what the correct fix would be but it should at least be documented under GDI+ this could be causing image corruption without anyone knowing. |
The source image format does not seem to have any affect on the issue, I tested png, bmp and jpg. I tried some different PixelFormats for the downscaled bitmap as well, but with unchanged outcome. Resizing the bitmap to an intermediate size, so it does not go beyond a 1/128th or 1/256th downscaling operation, for example downscaling to 1/64th first and then to the smaller size, works as a workaround. (But if I remember correctly, multiple resizing operations can cause artifacts. I consider it questionable how much this matters, but generally speaking it may be something to keep in mind.) |
It seems to me that rather than throwing an exception if scale factor is too large, it is better the do 2 scaling or if possible, use the HighQuality versions which {without testing) is probably faster. Since the current results is corrupt both solutions are better. |
.NET version
8.0
Did it work in .NET Framework?
No
Did it work in any of the earlier releases of .NET Core or .NET 5+?
Unlikely
Issue description
The result of a resizing operation, for example via the Bitmap-constructor or using Graphics, becomes corrupted when an image is resized to less than 1/128th (using Bilinear interpolation) or 1/256th (using Bicubic) of the original size.
The resulting image turns transparent and colors become corrupted, and as the scale goes beyond the point where corruption starts, the output changes.
I have conducted tests for this with different sized images, 16384x16384, 16384x100, 2048x2048, 1024x1024 for example, resizing width and/or height, all with the results being faulty.
A 16384x16384 test image resized to 128x128:
The same resized to 127x127, it is barely visible:
Resized to 64x64, transparency is slightly reduced at 247 instead of 255:
My assumption is that resizing by that much causes overflowing in the interpolation algorithms, leading to higher bytes being modified when they should not be.
This is likely an issue in the underlying GDI+ library, but I do not know where else to report this bug.
I was unable to find any information on this issue.
Steps to reproduce
Resize an image to less than 1/128th of its original width and/or height, either using the Bitmap(Bitmap height, int width, int height)-constructor or via Graphics, using InterpolationMode.Bilinear (or Default).
Or resize it to less than 1/256th when using Bicubic interpolation.
Code example:
The text was updated successfully, but these errors were encountered: