diff --git a/src/ImageSharp/Formats/Png/IPngEncoderOptions.cs b/src/ImageSharp/Formats/Png/IPngEncoderOptions.cs
index 3f5b33c0c8..0008080d3f 100644
--- a/src/ImageSharp/Formats/Png/IPngEncoderOptions.cs
+++ b/src/ImageSharp/Formats/Png/IPngEncoderOptions.cs
@@ -40,6 +40,11 @@ namespace ImageSharp.Formats
///
IQuantizer Quantizer { get; }
+ ///
+ /// Gets the transparency threshold.
+ ///
+ byte Threshold { get; }
+
///
/// Gets a value indicating whether this instance should write
/// gamma information to the stream.
diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs
index 1e2203e365..e6ade1df03 100644
--- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs
+++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs
@@ -495,7 +495,7 @@ namespace ImageSharp.Formats
// Grab the palette and write it to the stream.
TColor[] palette = quantized.Palette;
- byte pixelCount = (byte)palette.Length;
+ byte pixelCount = palette.Length.ToByte();
// Get max colors for bit depth.
int colorTableLength = (int)Math.Pow(2, header.BitDepth) * 3;
@@ -518,6 +518,11 @@ namespace ImageSharp.Formats
colorTable[offset + 1] = bytes[1];
colorTable[offset + 2] = bytes[2];
+ if (alpha > this.options.Threshold)
+ {
+ alpha = 255;
+ }
+
anyAlpha = anyAlpha || alpha < 255;
alphaTable[i] = alpha;
}
diff --git a/src/ImageSharp/Formats/Png/PngEncoderOptions.cs b/src/ImageSharp/Formats/Png/PngEncoderOptions.cs
index 3c5bb1f622..90175c6d65 100644
--- a/src/ImageSharp/Formats/Png/PngEncoderOptions.cs
+++ b/src/ImageSharp/Formats/Png/PngEncoderOptions.cs
@@ -57,6 +57,11 @@ namespace ImageSharp.Formats
///
public IQuantizer Quantizer { get; set; }
+ ///
+ /// Gets or sets the transparency threshold.
+ ///
+ public byte Threshold { get; set; } = 255;
+
///
/// Gets or sets a value indicating whether this instance should write
/// gamma information to the stream. The default value is false.
diff --git a/tests/ImageSharp.Tests/Formats/Png/PngSmokeTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngSmokeTests.cs
index 4bdfd288c5..2ba570453c 100644
--- a/tests/ImageSharp.Tests/Formats/Png/PngSmokeTests.cs
+++ b/tests/ImageSharp.Tests/Formats/Png/PngSmokeTests.cs
@@ -13,6 +13,7 @@ namespace ImageSharp.Tests.Formats.Png
using ImageSharp.Formats;
using System.Linq;
using ImageSharp.IO;
+ using System.Numerics;
public class PngSmokeTests
{
@@ -67,15 +68,19 @@ namespace ImageSharp.Tests.Formats.Png
using (Image source = provider.GetImage())
using (MemoryStream ms = new MemoryStream())
{
- // image.Save(provider.Utility.GetTestOutputFileName("bmp"));
source.MetaData.Quality = 256;
- source.Save(ms, new PngEncoder());
+ source.Save(ms, new PngEncoder(), new PngEncoderOptions {
+ Threshold = 200
+ });
ms.Position = 0;
using (Image img1 = Image.Load(ms, new PngDecoder()))
{
using (MemoryStream ms2 = new MemoryStream())
{
- img1.Save(ms2, new PngEncoder());
+ img1.Save(ms2, new PngEncoder(), new PngEncoderOptions
+ {
+ Threshold = 200
+ });
ms2.Position = 0;
using (Image img2 = Image.Load(ms2, new PngDecoder()))
{
@@ -90,8 +95,6 @@ namespace ImageSharp.Tests.Formats.Png
}
}
}
- // img2.Save(provider.Utility.GetTestOutputFileName("bmp", "_loaded"), new BmpEncoder());
- ImageComparer.CheckSimilarity(source, img2);
}
}
}