diff --git a/src/ImageSharp/Formats/Png/PngMetadata.cs b/src/ImageSharp/Formats/Png/PngMetadata.cs index a7b730ec9f..cecdf88c9d 100644 --- a/src/ImageSharp/Formats/Png/PngMetadata.cs +++ b/src/ImageSharp/Formats/Png/PngMetadata.cs @@ -230,7 +230,24 @@ public FormatConnectingMetadata ToFormatConnectingMetadata() /// 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();