Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Crash (AccessViolationException/ExecutionEngineException) graphics.FillRectangle into Bitmap with Format24bppRgb #14113

@deng0

Description

@deng0

.NET version

The crash happens in all .NET Core Version I have tested (8/9/10).
In .NET 8.0 it's an AccessViolationException and in 9/10 it's an ExecutionEngineException.

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

When not debugging the application just crashes.
When debugging you get an AccessViolationException/ExecutionEngineException on the graphics.FillRectangle command. But it's not just FillRectangle. The problem also occurs when using DrawImage with a similar target rectangleF.
I suppose there is a serious bug in System.Drawing.Drawing2D/GDI, that it tries to set pixel values outside the bounds of the bitmap data.

Steps to reproduce

Create simple winforms app and add the following code:

            for (int i = 0; i < 100; i++)
            {
                using (var bmp = new Bitmap(256, 256, PixelFormat.Format24bppRgb))
                using (var graphics = Graphics.FromImage(bmp))
                {
                    graphics.SmoothingMode = SmoothingMode.AntiAlias;
                    graphics.FillRectangle(Brushes.Green, new RectangleF(190.5f, 180.5f, 100, 100)); // <- Crash
                }
            }

Most of the time the crash occurs on first attempt, but not everytime, that's why I added the loop.

It seems the crash only occurs with the specific combination of PixelFormat.Format24bppRgb, SmoothingMode.AntiAlias and a Rectangle that exceeds the bounds of the Bitmap and uses non-integer coordinates.

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions