dotnet / dotnet/winforms

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

Open
#14,113 10 comments 0 reactions 1 assignee Claimed by @merriemcgaw View on GitHub
area-System.Drawing
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
1d 13m
Merged PRs (30d)
85

Description

### .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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.