From dd06da4c3764a7dc1dd58f8bade4b6b550d9c405 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 5 Aug 2025 12:34:32 +1000 Subject: [PATCH 1/2] Fix #2974 --- src/ImageSharp/Formats/Png/PngMetadata.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Png/PngMetadata.cs b/src/ImageSharp/Formats/Png/PngMetadata.cs index a7b730ec9..8768d8946 100644 --- a/src/ImageSharp/Formats/Png/PngMetadata.cs +++ b/src/ImageSharp/Formats/Png/PngMetadata.cs @@ -230,7 +230,24 @@ public class PngMetadata : IFormatMetadata /// public void AfterImageApply(Image destination, Matrix4x4 matrix) where TPixel : unmanaged, IPixel - => this.ColorTable = null; + { + this.ColorTable = null; + + // If the color type is RGB and we have a transparent color, we need to switch to RGBA + // so that we do not incorrectly preserve the obsolete tRNS chunk. + if (this.ColorType == PngColorType.Rgb && this.TransparentColor.HasValue) + { + this.ColorType = PngColorType.RgbWithAlpha; + this.TransparentColor = null; + } + + // The same applies for Grayscale. + if (this.ColorType == PngColorType.Grayscale && this.TransparentColor.HasValue) + { + this.ColorType = PngColorType.GrayscaleWithAlpha; + this.TransparentColor = null; + } + } /// IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone(); From 20975bcd8b7dd3e6e0634b79256ddf2587e071c6 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 5 Aug 2025 12:36:52 +1000 Subject: [PATCH 2/2] Update src/ImageSharp/Formats/Png/PngMetadata.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/ImageSharp/Formats/Png/PngMetadata.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Png/PngMetadata.cs b/src/ImageSharp/Formats/Png/PngMetadata.cs index 8768d8946..cecdf88c9 100644 --- a/src/ImageSharp/Formats/Png/PngMetadata.cs +++ b/src/ImageSharp/Formats/Png/PngMetadata.cs @@ -234,7 +234,7 @@ public class PngMetadata : IFormatMetadata this.ColorTable = null; // If the color type is RGB and we have a transparent color, we need to switch to RGBA - // so that we do not incorrectly preserve the obsolete tRNS chunk. + // so that we do not incorrectly preserve the obsolete tRNS chunk. if (this.ColorType == PngColorType.Rgb && this.TransparentColor.HasValue) { this.ColorType = PngColorType.RgbWithAlpha;