Graphics.Clear not working with alpha 0 values
See original GitHub issueDescription
The Clear(System.Drawing.Color color) method of System.Drawing.Graphics does not set the RGB values correctly when a color with alpha 0 is used.
Reproduction Steps
This unit test will fail:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Drawing;
using System.Drawing.Imaging;
[TestMethod]
public void ClearlyNotWorking()
{
var image = new Bitmap(3, 3, PixelFormat.Format32bppArgb);
Color initialColor = image.GetPixel(0, 0);
Assert.AreEqual(0, initialColor.A);
Assert.AreEqual(0, initialColor.R);
Assert.AreEqual(0, initialColor.G);
Assert.AreEqual(0, initialColor.B);
using (var graphics = Graphics.FromImage(image))
{
// Color.Transparent is A=0, R=255, G=255, B=255
graphics.Clear(Color.Transparent);
}
Color newColor = image.GetPixel(0, 0);
Assert.AreEqual(Color.Transparent.A, newColor.A);
// Expecting 255 on the RGB values, but actually they remain 0.
Assert.AreEqual(Color.Transparent.R, newColor.R);
Assert.AreEqual(Color.Transparent.G, newColor.G);
Assert.AreEqual(Color.Transparent.B, newColor.B);
}
Expected behavior
I expect all pixels in the bitmap to have exactly the color specified in the System.Drawing.Graphics.Clear(System.Drawing.Color color) method.
Actual behavior
RGB values remain 0 when a color with alpha 0 is provided to the System.Drawing.Graphics.Clear(System.Drawing.Color color) method.
Regression?
No response
Known Workarounds
You can work around this by:
- Locking the bitmap and getting the
BitmapData. - Manually changing all values in the data
- Unlocking the bitmap
Configuration
Tested and found the issue in:
- .NET 7 console application
- .NET Framework WPF application.
I’m on Windows 10 Pro 21H2 19044.2251 (x64), although I don’t think that matters for this issue.
Other information
No response
Issue Analytics
- State:
- Created 3 months ago
- Comments:8 (6 by maintainers)
Top Results From Across the Web
Why does System.Drawing.Graphics blank the RGB ...
Color values are defined as non-premultiplied, so gdiplus multiplies the components by the alpha value when it clears the foreground image.
Read more >How to clear a Graphics object?
The problem is that I need to clear this object for every loop, reseting all values ... Background() will reset colors to 0,...
Read more >Graphics.Clear(Color) Method (System.Drawing)
Remarks. The Clear method clears the state of the graphics object and should not be called when the graphics object cannot be updated....
Read more >love.graphics.clear
Note that the scissor area bounds the cleared region. In versions prior to 11.0, color component values were within the range of 0...
Read more >Add Transparency to Graphics Objects - MATLAB & Simulink
A value of 0 means completely transparent, a value of 1 means completely opaque, and values between 0 and 1 are semitransparent. Graphics...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I’ve made a proposal myself. Closing this issue.
@elachlan We generally caution people on opening PRs like this without an API that there has been consensus on. We don’t want to unnecessarily waste people’s time or disappoint them if there isn’t agreement on moving forward. In this particular case it’s a difficult thing to accomplish as there are so many supported pixel formats.