question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Graphics.Clear not working with alpha 0 values

See original GitHub issue

Description

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:closed
  • Created 3 months ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremyKuhnecommented, Jun 22, 2023

I’ve made a proposal myself. Closing this issue.

1reaction
JeremyKuhnecommented, Jun 19, 2023

@JPasterkampRotec thanks for the sample code! You’re welcome to open a doc PR that clarifies that Clear is effectively “fill with a solid brush”. There is an edit button on the doc page that will let you do so.

I’d suggest looking at submitting a PR to provide the functionality if you think other people will find it useful.

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found