diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ToolStrips/ToolStripRenderer.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/ToolStrips/ToolStripRenderer.cs index 5ce502caaaf..b62c48db870 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/ToolStrips/ToolStripRenderer.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/ToolStrips/ToolStripRenderer.cs @@ -856,27 +856,50 @@ protected virtual void OnRenderItemCheck(ToolStripItemImageRenderEventArgs e) return; } + // Track images we allocate so they can be disposed after drawing. + Image? disabledImage = null; + Image? invertedImage = null; + if (e.Item is not null) { if (!e.Item.Enabled) { - image = CreateDisabledImage(image, e.ImageAttributes); + disabledImage = CreateDisabledImage(image, e.ImageAttributes); + image = disabledImage; } - if (SystemInformation.HighContrast && image is Bitmap bitmap) + if (image is Bitmap bitmap) { - Color backgroundColor = e.Item.Selected ? SystemColors.Highlight : e.Item.BackColor; + if (SystemInformation.HighContrast) + { + Color backgroundColor = e.Item.Selected ? SystemColors.Highlight : e.Item.BackColor; - if (ControlPaint.IsDark(backgroundColor)) + if (ControlPaint.IsDark(backgroundColor)) + { + invertedImage = ControlPaint.CreateBitmapWithInvertedForeColor(bitmap, backgroundColor); + image = invertedImage; + } + } + else if (Application.IsDarkModeEnabled) { - Image invertedImage = ControlPaint.CreateBitmapWithInvertedForeColor(bitmap, e.Item.BackColor); - image = invertedImage; + Color backgroundColor = e.Item.Selected ? SystemColors.Highlight : e.Item.BackColor; + + if (ControlPaint.IsDark(backgroundColor)) + { + // In dark mode, the check mark icons are dark glyphs designed for light + // backgrounds. Invert the foreground color so they are visible on dark backgrounds. + invertedImage = ControlPaint.CreateBitmapWithInvertedForeColor(bitmap, backgroundColor); + image = invertedImage; + } } } } e.Graphics.DrawImage(image, imageRect, 0, 0, imageRect.Width, imageRect.Height, GraphicsUnit.Pixel, e.ImageAttributes); + + disabledImage?.Dispose(); + invertedImage?.Dispose(); } ///